排序父节点XML取决于子节点值 [英] Sort parent nodes XML depends on child nodes values
问题描述
我有以下XML
<Root>
<Element A/>
<Element B/>
<Data1>
<DataElement/>
<Values>
<Value>2222</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>ABC</Value>
<Name>field2</Name>
</Values>
</Data1>
<Data2>
<DataElement/>
<Values>
<Value>1111</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>XYZ</Value>
<Name>field2</Name>
</Values>
</Data2>
<DataN>
...
</DataN>
</Root>
例如,我需要获取具有Data部分的XML,该XML部分按指定字段名称的值"排序: 按"field1"排序将返回
I need to get the same XML with Data section sorted by "Value" of specified field name, for exmple: sort by "field1" will return
<Root>
<Element A/>
<Element B/>
<Data2>
<DataElement/>
<Values>
<Value>1111</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>XYZ</Value>
<Name>field2</Name>
</Values>
</Data2>
<Data1>
<DataElement/>
<Values>
<Value>2222</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>ABC</Value>
<Name>field2</Name>
</Values>
</Data1>
<DataN>
...
</DataN>
</Root>
此外,我必须发送排序字段的名称作为参数...
Also, I have to send name of sort field as parameter...
推荐答案
此转换完全实现了规定的要求.要特别注意保留不排序元素的确切顺序.目前没有其他答案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="vField" select="'field1'"/>
<xsl:param name="pSortType" select="'number'"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(),'Data')]">
<xsl:variable name="vPos" select=
"count(preceding-sibling::*[starts-with(name(),'Data')])+1"/>
<xsl:for-each select="/*/*[starts-with(name(),'Data')]">
<xsl:sort select="Values[Name=$vField]/Value"
data-type="{$pSortType}"/>
<xsl:if test="position() = $vPos">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
应用于以下XML文档(与提供的XML文档相同),但在Data1
和Data2
之间插入了附加的<Element C=""/>
,以便我们可以验证顺序的保留未排序的元素):
When applied on the following XML document (the same as the provided one, but with an additional <Element C=""/>
inserted between Data1
and Data2
so that we can verify the preservation of ordering of the non-sorted elements):
<Root>
<Element A=""/>
<Element B=""/>
<Data1>
<DataElement/>
<Values>
<Value>2222</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>ABC</Value>
<Name>field2</Name>
</Values>
</Data1>
<Element C=""/>
<Data2>
<DataElement/>
<Values>
<Value>1111</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>XYZ</Value>
<Name>field2</Name>
</Values>
</Data2>
</Root>
产生所需的正确结果-请注意,<Element C=""/>
的位置已保留:
produces the wanted, correct result -- note that the position of <Element C=""/>
is preserved:
<Root>
<Element A=""/>
<Element B=""/>
<Data2>
<DataElement/>
<Values>
<Value>1111</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>XYZ</Value>
<Name>field2</Name>
</Values>
</Data2>
<Element C=""/>
<Data1>
<DataElement/>
<Values>
<Value>2222</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>ABC</Value>
<Name>field2</Name>
</Values>
</Data1>
</Root>
这篇关于排序父节点XML取决于子节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!