XML到XLS复制元素末尾的当前元素

时间:2013-10-02 01:45:35

标签: xml xslt-1.0

   <?xml version="1.0" encoding="UTF-16"?>
<items>
    <item>
        <count>3</count>
        <name>Name</name>
        <description>Description</description>
    </item>
</items>

根据计数,最后需要追加2个额外的孩子。如果计数是3加两个孩子。请帮我写一下XLS给我。我是XSLT的新手。 期望的输出:

   <?xml version="1.0" encoding="UTF-16"?>
<items>
    <item>
        <count>1</count>
        <name>Name</name>
        <description>Description</description>
    </item>
    <item>
        <count>1</count>
        <name>Name</name>
        <description>Description</description>
    </item>
    <item>
        <count>1</count>
        <name>Name</name>
        <description>Description</description>
    </item>
</items>

2 个答案:

答案 0 :(得分:0)

在xslt 1.0中,递归模板应该是这样的,比如

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <items>
            <xsl:apply-templates select="items/item" />
        </items>
    </xsl:template>

    <xsl:template match="item">
        <!-- Call multiplication template for current item -->
        <xsl:call-template name="multiply">
            <xsl:with-param name="item" select="." />
        </xsl:call-template>
    </xsl:template>

    <!-- recursive named template to ensure multiplication -->
    <xsl:template name="multiply">
        <xsl:param name="item" />
        <!-- If count is not passed in as a parameter take it from item param -->
        <xsl:param name="count" select="$item/count"/>

        <item>
            <count>1</count>
            <xsl:copy-of select="$item/name" />
            <xsl:copy-of select="$item/description" />
        </item>

        <!-- Check if still needed to multiplicate -->
        <xsl:if test="$count &gt; 1">
            <!-- Recursive call-->
            <xsl:call-template name="multiply">
                <!-- for the same item -->
                <xsl:with-param name="item" select="$item" />
                <!-- but decreasing number of copies -->
                <xsl:with-param name="count" select="$count - 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

可能应检查计数是否为零或负数或空或不同类型或数字......但原则应相同。

答案 1 :(得分:0)

首先,如果您需要使用XSLT,我建议您阅读一些教程以帮助您理解XSLT的语法。例如,在W3Schools上做教程。

这是一个为您提供所需输出的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-16" indent="yes"/>

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

    <!-- When <count> equals 3, create to extra <item> elements, with the same value of this current <item>, but default <count> to 1 -->
    <xsl:template match="item[count=3]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
        <item>
            <count>1</count>
            <name><xsl:value-of select="name" /></name>
            <description><xsl:value-of select="description" /></description>
        </item>
        <item>
            <count>1</count>
            <name><xsl:value-of select="name" /></name>
            <description><xsl:value-of select="description" /></description>
        </item>
    </xsl:template>

    <!-- Reset the <count>3</count> element to value 1 -->
    <xsl:template match="count[text()=3]">
        <xsl:copy>
            <xsl:text>1</xsl:text>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

如果你想在<count>元素上以递归和独立的方式完成所有操作,但在<count>大于1时始终执行它,那么请使用下一个模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

    <!-- When <count> is greater than 1, duplicate the <item> elements occording to the <count>, with the same value of this current <item>, but default <count> to 1 -->
    <xsl:template match="item[count &gt; 1]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
        <xsl:call-template name="multiply">
            <xsl:with-param name="count" select="count" />
            <xsl:with-param name="name" select="name" />
            <xsl:with-param name="description" select="description" />
        </xsl:call-template>
    </xsl:template>

    <!-- Reset the <count> element to value 1, when the <count> element is greater than 1 -->
    <xsl:template match="count[text() &gt; 1]">
        <xsl:copy>
            <xsl:text>1</xsl:text>
        </xsl:copy>
    </xsl:template>

    <!-- Recursive template -->
    <xsl:template name="multiply">
        <xsl:param name="count" />
        <xsl:param name="name" />
        <xsl:param name="description" />

        <xsl:if test="$count &gt; 1">
            <item>
                <count>1</count>
                <name><xsl:value-of select="$name" /></name>
                <description><xsl:value-of select="$description" /></description>
            </item>

            <xsl:call-template name="multiply">
                <xsl:with-param name="count" select="$count - 1" />
                <xsl:with-param name="name" select="$name" />
                <xsl:with-param name="description" select="$description" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>