【发布时间】:2014-05-30 11:54:49
【问题描述】:
我尝试根据给定 xml 的值对 XML 进行排序。从请求 xml 我需要根据education_details {DR,PDR,MSC,BSC}进行排序。我有用。请参阅下面的示例。
输入 xml
<?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>
转换 XSLT
<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
<?xml version="1.0" encoding="UTF-8"?>
<lookups>
<lookup tc="13" responce="Doctor" request="DR" />
<lookup tc="12" responce="Post DR" request="PDR" />
<lookup tc="30" responce="Master of Scienc" request="MSc" />
<lookup tc="4" responce="Bachelor of Science" request="BSc" />
</lookups>
输出xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<document>
<studentname>ACM</studentname>
<stu_educational_details tc="30">Master of Scienc</stu_educational_details>
</document>
<document>
<studentname>ACB</studentname>
<stu_educational_details tc="4">Bachelor of Science</stu_educational_details>
</document>
<document>
<studentname>ACP</studentname>
<stu_educational_details tc="12">Post DR</stu_educational_details>
</document>
<document>
<studentname>ACC</studentname>
<stu_educational_details tc="13">Doctor</stu_educational_details>
</document>
</root>
预期输出
<?xml version="1.0" encoding="UTF-8"?>
<root>
<document>
<studentname>ACC</studentname>
<stu_educational_details tc="13">Doctor</stu_educational_details>
</document>
<document>
<studentname>ACP</studentname>
<stu_educational_details tc="12">Post DR</stu_educational_details>
</document>
<document>
<studentname>ACM</studentname>
<stu_educational_details tc="30">Master of Scienc</stu_educational_details>
</document>
<document>
<studentname>ACB</studentname>
<stu_educational_details tc="4">Bachelor of Science</stu_educational_details>
</document>
</root>
我编写了更改元素和值名称的逻辑。
【问题讨论】:
-
使用样式表转换的 Inout 文件不会产生输出文件的确切结构。你能展示你的实际 XSLT 吗?不过,使用样式表转换输入文件确实会提供正确的顺序......因此,要么在运行转换时覆盖 pOrder 参数,要么在实际样式表中存在另一个问题。
-
我认为覆盖 pOrder 参数的问题。