Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过xslt重新排列xml节点_Xml_Xslt - Fatal编程技术网

通过xslt重新排列xml节点

通过xslt重新排列xml节点,xml,xslt,Xml,Xslt,嗨,我是XSLT的初学者。我有一个具有不同属性的XMl,我的目标是创建一个具有相应属性顺序模式的新XMl。为此,我使用以下XSLT <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">

嗨,我是XSLT的初学者。我有一个具有不同属性的XMl,我的目标是创建一个具有相应属性顺序模式的新XMl。为此,我使用以下XSLT

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
                <channel>
                    <title>Test Data</title>
                    <link>http://www.google.com</link>
                    <xsl:apply-templates select="products/product" />

                </channel>
        </xsl:template>

        <xsl:template match="products/product">
            <item>
                <xsl:apply-templates select="@*"/>
            </item>
        </xsl:template>

        <xsl:template match="products/product/@*">
            <xsl:copy-of select="."/>
        </xsl:template>

        <xsl:template match="products/product/Parent_identifier | products/product/Unique_ID | products/product/EAN> 
            <xsl:element name="{local-name()}">
                <xsl:apply-templates select="node()|@*" />
            </xsl:element>
        </xsl:template>

    </xsl:stylesheet>

测试数据
http://www.google.com

此XML中没有属性。示例输入/输出显示您正在尝试更改元素的顺序。因此,样式表中对
@*
的大多数使用应该更改为
*

要以与输入不同的顺序输出元素,请在应用模板中显式选择它们:

XSLT2.0/3.0

<xsl:apply-templates select="Parent_identifier, Unique_ID, EAN"/>
<xsl:apply-templates select="Parent_identifier"/>
<xsl:apply-templates select="Unique_ID"/>
<xsl:apply-templates select="EAN"/>

XSLT1.0

<xsl:apply-templates select="Parent_identifier, Unique_ID, EAN"/>
<xsl:apply-templates select="Parent_identifier"/>
<xsl:apply-templates select="Unique_ID"/>
<xsl:apply-templates select="EAN"/>


我建议您花一个小时学习XSLT教程。这应该是完成这项琐碎任务所需的全部内容了。感谢您的建议-Michaelth此XML中没有属性。示例输入/输出显示您正在尝试更改元素的顺序。因此,样式表中使用的大部分
@*
都应更改为
*
。在将@*替换为*my xml后,将转换为my xml,但元素的顺序也不例外。我已经厌倦了XSLT 1.0的建议。如果您已经纠正了一个问题,现在又转到另一个问题,那么最好是提出一个新问题。StackOverflow格式不允许这样的补充问题。从头开始:给出您的输入、预期输出和当前XSLT代码。