我尝试根据给定xml的值对XML进行排序。在这个xml中,我需要根据education_details {DR,PDR,MSC,BSC}进行排序。我用过。请看下面的示例。
输入XML: -
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<document>
<studentname>ACM</studentname>
<educational_details>MSC</educational_details>
</document>
<document>
<studentname>ACB</studentname>
<educational_details>BSc</educational_details>
</document>
<document>
<studentname>ACP</studentname>
<educational_details>PDR</educational_details>
</document>
<document>
<studentname>ACC</studentname>
<educational_details>DR</educational_details>
</document>
</Person>
我尝试过如下所示。
XSL: -
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="pOrder" select="'DR,PDR,MSC,BSc'" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:sort
select="string-length(
substring-before(
concat(',',$pOrder,','),
concat(',',name(),',')))" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Out put
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<document>
<studentname>ACM</studentname>
<educational_details>MSC</educational_details>
</document>
<document>
<studentname>ACB</studentname>
<educational_details>BSc</educational_details>
</document>
<document>
<studentname>ACP</studentname>
<educational_details>PDR</educational_details>
</document>
<document>
<studentname>ACC</studentname>
<educational_details>DR</educational_details>
</document>
</Person>
预期结果: -
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<document>
<studentname>ACC</studentname>
<educational_details>DR</educational_details>
</document>
<document>
<studentname>ACP</studentname>
<educational_details>PDR</educational_details>
</document>
<document>
<studentname>ACM</studentname>
<educational_details>MSC</educational_details>
</document>
<document>
<studentname>ACB</studentname>
<educational_details>BSc</educational_details>
</document>
</Person>
答案 0 :(得分:2)
我认为你想要类似的东西
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="pOrder" select="'DR,PDR,MSC,BSc'" />
<xsl:variable name="vSequence" select="tokenize($pOrder, ',')"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="document">
<xsl:sort
select="index-of($vSequence, educational_details)" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
改变
<?xml version="1.0" encoding="UTF-8"?>
<root>
<document>
<studentname>ACM</studentname>
<educational_details>MSC</educational_details>
</document>
<document>
<studentname>ACB</studentname>
<educational_details>BSc</educational_details>
</document>
<document>
<studentname>ACP</studentname>
<educational_details>PDR</educational_details>
</document>
<document>
<studentname>ACC</studentname>
<educational_details>DR</educational_details>
</document>
</root>
到
<?xml version="1.0" encoding="UTF-8"?><root><document>
<studentname>ACC</studentname>
<educational_details>DR</educational_details>
</document><document>
<studentname>ACP</studentname>
<educational_details>PDR</educational_details>
</document><document>
<studentname>ACM</studentname>
<educational_details>MSC</educational_details>
</document><document>
<studentname>ACB</studentname>
<educational_details>BSc</educational_details>
</document></root>