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
XML元素排序_Xml_Xslt - Fatal编程技术网

XML元素排序

XML元素排序,xml,xslt,Xml,Xslt,我有以下XML文件: <?xml version="1.0" encoding="UTF-8"?> <table> <row> <description2>ABC</description2> </row> <row> <description2>DE

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
        <table>
            <row>
                <description2>ABC</description2>
            </row>
            <row>
                <description2>DEF</description2>
            </row>
            <row>
                <description2>GHI</description2>
            </row>
            <row>
                <description1>JKL</description1>
                <message>msg1</message>
            </row>
            <row>
                <description1>MNO</description1>
                <message>msg2</message>
            </row>
        </table>

基础知识
DEF
GHI
JKL
msg1
MNO
msg2
我希望所有包含description2子节点的行节点都显示在包含description的行节点之后

因此,生成的XML如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<table>
    <row>
        <description1>JKL</description1>
        <message>msg1</message>
    </row>
    <row>
        <description1>MNO</description1>
        <message>msg2</message>
    </row>
    <row>
        <description2>ABC</description2>
    </row>
    <row>
        <description2>DEF</description2>
    </row>
    <row>
        <description2>GHI</description2>
    </row>
</table>

JKL
msg1
MNO
msg2
基础知识
DEF
GHI

如何在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-8" indent="yes"/>

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

    <xsl:template match="table">
        <xsl:apply-templates select="row/description1/.."/>
        <xsl:apply-templates select="row/description2/.."/>
    </xsl:template>
</xsl:stylesheet>
第一个排序键是第一个子标记的名称(
description…
),第二个是它的值


如果您不需要此功能,只需删除第二条
排序
指令。

您可以编辑您的问题以显示您尝试过的XSLT吗?谢谢。@Tim C嗨,我不知道XSLT。如果你能满足我的要求,请帮助我。谢谢。@timchi,我不知道XSLT。如果你能满足我的要求,请帮助我。谢谢。尝试搜索和,尝试自己创建XSLT,然后在遇到问题时更新您的问题。
<xsl:template match="table">
   <xsl:for-each select="row">
        <xsl:sort select="*[1]/name()" />
        <xsl:sort select="*[1]" />
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:for-each>
</xsl:template>