使用<;根据节点值对XML进行排序;xsl:sort>;并更改元素名称问题
我尝试根据给定XML的值对XML进行排序。从请求xml中,我需要基于教育详细信息{DR、PDR、MSC、BSC}进行排序。我有用。请看下面的样品 输入xml使用<;根据节点值对XML进行排序;xsl:sort>;并更改元素名称问题,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我尝试根据给定XML的值对XML进行排序。从请求xml中,我需要基于教育详细信息{DR、PDR、MSC、BSC}进行排序。我有用。请看下面的样品 输入xml <?xml version="1.0" encoding="UTF-8"?> <root> <document> <studentname>ACM</studentname> <educational_details>M
<?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>
ACM
移动交换中心
ACB
理学士
机场核心计划
PDR
行政协调会
博士
转换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>
ACM
科学硕士
ACB
理学士
机场核心计划
博士后
行政协调会
医生
预期产量
<?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>
行政协调会
医生
机场核心计划
博士后
ACM
科学硕士
ACB
理学士
我编写了更改元素和值名称的逻辑。您发布的样式表是我建议的样式表,作为对您前面问题的回答,它可以进行排序,但不会产生您想要的重命名和替换 重命名很容易添加:
<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:template match="educational_details">
<stu_educational_details>
<xsl:value-of select="."/>
</stu_educational_details>
</xsl:template>
</xsl:stylesheet>
请注意,当前您的示例与往常一样使用不同的拼写(
response
,而不是response
),并且在请求
属性值和一些示例数据中使用不同大小写的字母,您可能需要规范拼写,或者需要添加
,例如键('code',大写(),$lookup doc)
使用样式表转换的Inout文件不会生成输出文件的确切结构。您可以显示实际的XSLT吗?但是,用样式表转换输入文件的顺序是正确的。。。因此,要么在运行转换时过度使用pOrder参数,要么在实际样式表中存在另一个问题。我认为过度使用pOrder参数的问题是正确的。我已经用查找xml编辑了这个问题。你能检查一下吗
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="lookup-url" select="'test2014053002.xml'"/>
<xsl:variable name="lookup-doc" select="doc($lookup-url)"/>
<xsl:param name="pOrder" select="'DR,PDR,MSC,BSc'" />
<xsl:variable name="vSequence" select="tokenize($pOrder, ',')"/>
<xsl:key name="codes" match="lookup" use="@request"/>
<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:template match="educational_details">
<stu_educational_details tc="{key('codes', ., $lookup-doc)/@tc}">
<xsl:value-of select="key('codes', ., $lookup-doc)/@responce"/>
</stu_educational_details>
</xsl:template>
</xsl:stylesheet>