Saturday, 12 December 2015

XML : Selecting from xml specifying an order

using xslt 1.0 on python, I am trying to select a few items while specifying order:

  <?xml version="1.0" encoding="UTF-8"?>  <items>  <item name='1'>  first  </item>  <item name='2'>  second  </item>  <item name='3'>  third  </item>  </items>    

If I use a for-each with a big OR'd together list, I can get the items I want, but only in the original order of the xml source document above.

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="/">    <html>    <body>          <xsl:for-each select="items/item[@name='2']|items/item[@name='1']">  <p>hi</p>  <xsl:value-of select="." />  </xsl:for-each>    </body>    </html>      </xsl:template>  </xsl:stylesheet>    

This produces:

  hi  first  hi  second    

But I would like to have it output:

  hi  second  hi  first    

I think using xsl:apply-templates might be the way to go, but I can't get it to work with even this simple example. What is the best way in xslt 1.0 to select elements in a specific order?

No comments:

Post a Comment