使用XSLT转换XML格式

使用XSLT转换XML格式,xslt,Xslt,我的要求是转换以下数组 <array> <value>755</value> <value>5861</value> <value>4328</value> </array> 我看到了“身份模板”的使用。但我还没用过 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform

我的要求是转换以下数组

<array>
    <value>755</value>
    <value>5861</value>
    <value>4328</value>
</array>
我看到了“身份模板”的使用。但我还没用过

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

    <xsl:template match="/array">
            <xsl:element name="array">
                    <xsl:for-each select="value">
                        <xsl:element name="int">
                            <xsl:value-of select="." />
                        </xsl:element>
                  </xsl:for-each>   
            </xsl:element>              
    </xsl:template>

</xsl:stylesheet>

您可以执行以下操作:

<xsl:template match="/array">
    <array>
        <xsl:for-each select="value">
            <int>
                <xsl:value-of select="." />
            </int>
        </xsl:for-each>   
    </array>              
</xsl:template>

以下是您链接的公认答案中的一句话:

XSL不能替代任何东西。你能做的最好的事情就是复制你想保留的零件,然后输出你想更改的零件,而不是你不想保留的零件


这就是身份模板发挥作用的地方:它复制所有不以另一个匹配模板为目标的内容。结果是,如果基础XML包含的内容不仅仅是数组,那么还应该在xslt中包含标识模板。但是,如果您确信您的xml将不包含任何其他内容,那么您就不需要它。

您当前的方法是有效的。它更像是一个样式表。“推送”样式使用应用模板

您可以通过使用元素文字将其缩短一点,从而使其更易于阅读:

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

    <xsl:template match="/array">
        <array>
            <xsl:for-each select="value">
                <int>
                    <xsl:value-of select="." />
                </int>
            </xsl:for-each>   
        </array>              
    </xsl:template>

</xsl:stylesheet>

你的代码比我的效率高。正确的?如果我们可以只放置标记,那么标记有什么用呢?在
中,您可以使用变量作为
@name
@namespace
。如果您知道元素的名称,并且不需要动态构造它,则通常更倾向于使用元素文字,因为它较短,并且“读取”类似于输出。如果我们可以只放置标记,那么标记的用途是什么?我已经更新了注释,解释了修改后的标识模板样式表中的两个模板。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/array">
        <array>
            <xsl:for-each select="value">
                <int>
                    <xsl:value-of select="." />
                </int>
            </xsl:for-each>   
        </array>              
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>
    <!--identity template, which copies every attribute and 
        node(element, text, comment, and processing instruction) 
        that it matches and then applies templates to all of it's 
        attributes and child nodes (if there are any) -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--Specialized template that matches the value element.
        Because it is more specific than the identity template above it has
        a higher priority and will match when the value element is encountered.
        It creates an int element and then applies templates to any attributes 
        and child nodes of the value element -->
    <xsl:template match="value">
        <int>
            <xsl:apply-templates select="@*|node()"/>
        </int>
    </xsl:template>

</xsl:stylesheet>