Xml 稍后使用xsl:number作为固定数字

Xml 稍后使用xsl:number作为固定数字,xml,xslt,numbers,xlrd,Xml,Xslt,Numbers,Xlrd,我有一个关于节点编号的问题。稍后,我想重新使用分配给节点的编号。我将通过一个例子来说明我的意思 xml代码: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="cookbook.xsl"?> <cookbook> <recipe> <name>Waffles</name> <category>Breakfast</category>

我有一个关于节点编号的问题。稍后,我想重新使用分配给节点的编号。我将通过一个例子来说明我的意思

xml代码:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="cookbook.xsl"?>
<cookbook>
<recipe>
<name>Waffles</name>
<category>Breakfast</category>
<description>Make the waffles</description>
</recipe>
<recipe>
<name>Spaghetti</name>
<category>Diner</category>
<description>Make the spaghetti</description>
</recipe>
<recipe>
<name>Pancakes</name>
<category>Breakfast</category>
<description>Make the pancakes</description>
</recipe>
</cookbook>
使用xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">

<xsl:for-each select="cookbook/recipe">
<xsl:variable name="number">
<xsl:number level="any" count="recipe" value="position()"/>
</xsl:variable>
<xsl:value-of select="$number"/>
<xsl:value-of select="name"/>
    <xsl:for-each select="description">
    <xsl:value-of select="." />
    </xsl:for-each>
</xsl:for-each>
xsl:


在后一个示例中,数字不再是计数器,而是属于每个recipename的固定数字。 由于我是xslt新手,我不知道如何搜索答案。在这里使用xsl:number可能不是正确的做法。我已经试了很多次了,但是我仍然不能理解这个问题

如果能给我一个正确方向的提示,我将不胜感激。提前感谢。

试试:

<xsl:value-of select="count(preceding-sibling::recipe)+1" />


顺便说一句,你应该阅读一般的键,特别是使用Muenchian方法进行分组

这很好用。xml文件中的每个配方节点都会编号。但如果我按字母顺序排列食谱节点,然后对它们进行编号,会怎么样?但我将首先研究使用键和Muenchian方法。也许答案就在那里。@TessMaes“但是如果我按字母顺序排列我的食谱节点,然后想对它们进行编号呢?”我想你会使用position()。
Breakfast
Waffles (1)
Pancakes (3)

Diner
Spaghetti (2)
<xsl:for-each select="//category[not(preceding::category=.)]">
<xsl:sort select="." />
<xsl:value-of select="." />
<xsl:variable name="categoryname">
    <xsl:value-of select="." />
</xsl:variable>
<xsl:for-each select="//category[$categoryname=.]/..">
    <a  class="index" href="#{generate-id(name)}">
        <xsl:value-of select="name" />
        <xsl:text> (</xsl:text>
        <!--I would like to show the number right here-->
        <xsl:text>)</xsl:text>
    </a>
</xsl:for-each>
<xsl:value-of select="count(preceding-sibling::recipe)+1" />