如何使用XSLT在XML文件中复制子元素n次

如何使用XSLT在XML文件中复制子元素n次,xml,xslt,transform,using,Xml,Xslt,Transform,Using,我对XSLT非常陌生。我需要将子节点转换和复制1000次,并增加id节点,使它们每次都不同 输入XML: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="test.xsl"?> <catalog> <cd> <id>2017</id> <artist>Bob

我对XSLT非常陌生。我需要将子节点转换和复制1000次,并增加id节点,使它们每次都不同

输入XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<catalog>
    <cd>
        <id>2017</id>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
</catalog>

2017
鲍勃·迪伦
美国
哥伦比亚
10.90
1985
我的XSLT:但它只复制一次

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


我需要的是:请帮忙

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl"   href="test.xsl"?>
<catalog>
    <cd>
        <id>2017</id>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
   </cd>
    <cd>
        <id>2018</id>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
   </cd>
   <cd>
        <id>2019</id>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
   </cd>

    <!-- 997 more times with ID increment +1 each time  -->

</catalog>

2017
鲍勃·迪伦
美国
哥伦比亚
10.90
1985
2018
鲍勃·迪伦
美国
哥伦比亚
10.90
1985
2019
鲍勃·迪伦
美国
哥伦比亚
10.90
1985

在XSLT1.0中,可以通过使用递归模板来实现这一点。因此,模板反复调用自身,每次递增一个参数,直到达到所需的限制

尝试此XSLT(根据需要,将参数5替换为1000)


如果我在match=“id”say match=“year”之后添加了另一个模板,并且确实添加了,那么我会得到NaN。知道为什么吗?
标签实际上包含一个数字,还是它是空的?年份包含数字而不是空的。我甚至尝试使用“format number(number()+$count+1,“0”)”,但我也得到了“NaN”。实际上,在我的真实XML文件中,标记不是“year”,而是“ReviewID”,它的值为1234,我也希望它增加1。您可能需要编辑您的问题,以包括您遇到问题的“ReviewID”字段。或者,如果您的实际XML与当前问题中显示的内容非常不同,那么最好提出一个全新的问题。谢谢Tim,为了简单起见,让我们假设我也希望将year标签设置为increment,或者甚至将相同的incremented值从“id”复制到“year”。谢谢你,蒂姆。
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl"   href="test.xsl"?>
<catalog>
    <cd>
        <id>2017</id>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
   </cd>
    <cd>
        <id>2018</id>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
   </cd>
   <cd>
        <id>2019</id>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
   </cd>

    <!-- 997 more times with ID increment +1 each time  -->

</catalog>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="total" select="5" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="cd" name="cd">
    <xsl:param name="count" select="1" />
    <xsl:copy>
        <xsl:apply-templates select="@*|node()">
            <xsl:with-param name="count" select="$count" />
        </xsl:apply-templates>
    </xsl:copy>
    <xsl:if test="$count &lt; $total">
        <xsl:apply-templates select=".">
            <xsl:with-param name="count" select="$count + 1" />
        </xsl:apply-templates>
    </xsl:if>
</xsl:template>

<xsl:template match="id">
    <xsl:param name="count" />
    <xsl:copy>
        <xsl:value-of select="number() + $count - 1" />
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="id|year">
    <xsl:param name="count" />
    <xsl:copy>
        <xsl:value-of select="number() + $count - 1" />
    </xsl:copy>
</xsl:template>