基于xml标记的属性值显示输出

基于xml标记的属性值显示输出,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我正在使用以下xslt 1. Manitoba 2. Newfoundland 3. British 4. New Brunswick 5. Alberta . 我知道这样做并没有给我想要的输出,但这是迄今为止我得到的 我想根据属性“num”值来定位它们,我该怎么做 我想根据属性“num”值来定位它们,我该怎么做 这种操作称为排序。您需要对xsl:apply templates中的输入元素进行排序: <?xml version="1.0" encoding="US-ASCII"

我正在使用以下xslt

1. Manitoba
2. Newfoundland  
3. British
4. New Brunswick
5. Alberta

. 
我知道这样做并没有给我想要的输出,但这是迄今为止我得到的

我想根据属性“num”值来定位它们,我该怎么做

我想根据属性“num”值来定位它们,我该怎么做

这种操作称为排序。您需要对
xsl:apply templates
中的输入元素进行排序:

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />

  <xsl:template match="provinces">
    <xsl:apply-templates select="name" />
  </xsl:template>

  <xsl:template match="name">
    <xsl:value-of select="position()" />
    <xsl:text>. </xsl:text>
    <xsl:value-of select="." />
  </xsl:template>

</xsl:stylesheet>
文本输出

<xsl:apply-templates select="name">
    <xsl:sort select="@num"/>
</xsl:apply-templates>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />

  <xsl:template match="provinces">
    <xsl:apply-templates select="name">
        <xsl:sort select="@num" data-type="number"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="name">
    <xsl:value-of select="concat(position(),'. ')" />
    <xsl:value-of select="." />
    <xsl:if test="position() != last()">
        <xsl:text>&#10;</xsl:text>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

最好是
,以确保以数字方式进行排序。否则更多的项目
10
将在
2
之前结束@MartinHonnen谢谢Martin,你说得对。我已经编辑了我的答案。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />

  <xsl:template match="provinces">
    <xsl:apply-templates select="name">
        <xsl:sort select="@num" data-type="number"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="name">
    <xsl:value-of select="concat(position(),'. ')" />
    <xsl:value-of select="." />
    <xsl:if test="position() != last()">
        <xsl:text>&#10;</xsl:text>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
1. Manitoba
2. Newfoundland
3. British
4. New Brunswick
5. Alberta