【问题标题】:How to copy child element n times in XML file using XSLT如何使用 XSLT 在 XML 文件中复制子元素 n 次
【发布时间】:2017-01-03 21:22:05
【问题描述】:

我对 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>

我的 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>

我需要的是:请帮忙

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

【问题讨论】:

    标签: xml xslt transform using


    【解决方案1】:

    在 XSLT 1.0 中,您可以通过使用递归模板来实现这一点。所以模板反复调用自己,每次递增一个参数,直到达到要求的限制。

    试试这个 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 &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>
    

    编辑:如果您想要相同的逻辑但对于cd 下的其他元素,只需修改匹配id 的模板以将它们包含在匹配中。比如……

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

    http://xsltransform.net/gWEamLv查看此操作

    【讨论】:

    • 如果我在 match="id" 之后添加另一个模板,说 match="year" 并执行 ,我得到 NaN。知道为什么吗?
    • &lt;year&gt; 标签实际上是否包含数字,还是为空?
    • 年份包含数字且不为空。我什至尝试使用'format-number(number() + $count + 1, "0")',但我也得到了"NaN"。实际上,在我的真实 XML 文件中,该标签不称为“年份”,而是称为“ReviewID”,它的值是 1234,我也想增加 1。
    • 您可能需要编辑您的问题以包含您遇到问题的“ReviewID”字段。或者,如果您的实际 XML 与当前问题中显示的内容完全不同,那么您最好提出一个全新的问题。谢谢!
    • Tim,为了简单起见,假设我想让年份标签也增加,或者甚至将相同的增加值从“id”复制到“year”。谢谢蒂姆。
    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    相关资源
    最近更新 更多