[英]How to copy child element n times in XML file using XSLT
I am very new to XSLT. 我对XSLT非常陌生。 I need to transform and copy child nodes 1000 times and also increment the id node so that they are different each time.
我需要将子节点转换和复制1000次,还需要增加id节点,以使它们每次都不同。
Input XML: 输入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>
My XSLT: but it only copies once 我的XSLT:但是它只能复制一次
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
What I need is: Please help 我需要的是:请帮忙
<?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>
In XSLT 1.0, you can achieve this by using a recursive template. 在XSLT 1.0中,您可以通过使用递归模板来实现。 So the template repeatedly calls itself, incrementing a parameter each time until it reaches the required limit.
因此,模板会反复调用自身,每次增加参数直到达到所需的限制。
Try this XSLT (replace the parameter 5 with 1000, as required) 尝试使用此XSLT(根据需要将参数5替换为1000)
<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 < $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>
EDIT: If you want the same logic but for other elements under cd
, simply amend the template matching id
to include them in the match. 编辑:如果您想要相同的逻辑,但对于
cd
下的其他元素,只需修改模板匹配id
即可将它们包括在匹配中。 For example.. 例如..
<xsl:template match="id|year">
<xsl:param name="count" />
<xsl:copy>
<xsl:value-of select="number() + $count - 1" />
</xsl:copy>
</xsl:template>
See this in action at http://xsltransform.net/gWEamLv 可以在http://xsltransform.net/gWEamLv上查看此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.