简体   繁体   中英

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?

You can use <xsl:sort> to specify ordering especially when there is a specific logic to define the ordering , for example to order by name attribute value in descending order :

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

xsltransform demo 1

"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"

That's also possible, for example :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
  <xsl:template match="/"> 
    <html> 
      <body> 
        <xsl:apply-templates select="items/item[@name='2']"/>
        <xsl:apply-templates select="items/item[@name='1']"/>
      </body> 
    </html> 
  </xsl:template> 

  <xsl:template match="items/item">
      <p>hi</p>  
      <xsl:value-of select="."/> 
  </xsl:template>
</xsl:stylesheet>

xsltransform demo 2

output :

<html>
   <body>
      <p>hi</p>
      second

      <p>hi</p>
      first

   </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM