2

我正在根据父亲中的一个字段对祖父母进行排序。源文件看起来像

<Root>
    <AllData>
        <Data_not_to_be_sorted>
            <Additional_data1>
                <Some_test_data1/>
                <Some_test_data2/>
            </Additional_data1>
        </Data_not_to_be_sorted>
        <RealData>
            <Some_data1/>
            <Some_data2/>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                    <Father>
                        <Value>4</Value>
                        <Name>name in 4</Name>
                    </Father>
            </GrandFather>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                <Father>
                    <Value>3</Value>
                    <Name>name in 3</Name>
                </Father>
            </GrandFather>
        </RealData>
        <RealData>
            <Some_data1/>
            <Some_data2/>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                <Father>
                    <Value>2</Value>
                    <Name>name in 2</Name>
                </Father>
            </GrandFather>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                <Father>
                    <Value>1</Value>
                    <Name>name in 1</Name>
                </Father>
            </GrandFather>
        </RealData>
    </AllData>
</Root>

我正在使用的 XSLT 代码如下:

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



<xsl:template match="RealData">
<xsl:copy>
<xsl:apply-templates select="Data_required_as_it_is"></xsl:apply-templates>
<xsl:apply-templates select="GrandFather">
<xsl:sort select="Father/Value" data-type="number"/>
</xsl:apply-templates>

</xsl:copy>

</xsl:template>

</xsl:stylesheet>

代码运行良好......我的查询是 - 我可以为

Data_required_as_it_is。想象一下,我有 20 个不同的 xml 标签用于“Data_required_as_it_is”,所以我需要手动编写所有这些标签,或者只是以通用方式编写它们......

代码的输出如下:

<Root>
    <AllData>
        <Data_not_to_be_sorted>
            <Additional_data1>
                <Some_test_data1/>
                <Some_test_data2/>
            </Additional_data1>
        </Data_not_to_be_sorted>
        <RealData>
            <Some_data1/>
            <Some_data2/>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                    <Father>
                        <Value>3</Value>
                        <Name>name in 3</Name>
                    </Father>
            </GrandFather>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                <Father>
                    <Value>4</Value>
                    <Name>name in 4</Name>
                </Father>
            </GrandFather>
        </RealData>
        <RealData>
            <Some_data1/>
            <Some_data2/>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                <Father>
                    <Value>1</Value>
                    <Name>name in 1</Name>
                </Father>
            </GrandFather>
            <GrandFather>
                <Data_required_as_it_is></Data_required_as_it_is>
                <Father>
                    <Value>2</Value>
                    <Name>name in 2</Name>
                </Father>
            </GrandFather>
        </RealData>
    </AllData>
</Root>
4

2 回答 2

2
<xsl:template match="RealData">
      <xsl:copy>
          <xsl:apply-templates select="Data_required_as_it_is"></xsl:apply-templates>
          <xsl:apply-templates select="GrandFather">
              <xsl:sort select="Father/Value" data-type="number"/>
          </xsl:apply-templates>
      </xsl:copy>
  </xsl:template>

可以使用这样的更通用的代码

<xsl:template match="RealData">
    <xsl:copy>
        <xsl:apply-templates>
            <xsl:sort select="self::GrandFather/Father/Value" data-type="number"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

注意GrandFather:这将在任何节点之前对任何非节点进行排序,并将按元素对元素进行GrandFather指定的排序。GrandFatherFather/Value

如果希望非GrandFather元素仍然散布在GrandFather元素中,这也可以实现 - 如果有兴趣,请提出另一个问题。

于 2012-05-07T13:18:04.710 回答
1

如果此时你想匹配任何不是GrandFather元素的东西,你应该能够做这样的事情:

<xsl:apply-templates select="*[not(self::GrandFather)]"></xsl:apply-templates> 

请注意,如果您在任何现有的GrandFather元素之后有元素,那么在这种情况下,它们将被移动到输出中已排序的GrandFather元素之前。

于 2012-05-07T12:40:31.000 回答