xslt 2.0按编​​号排序元素

时间:2016-04-27 10:16:11

标签: sorting xslt

我有以下输入XML, 由于某种原因,我的xslt无法对其进行排序。 请指教。 感谢您的见解。 这是完整的XML .................................................. ....

我想根据D_628字段对G_SHL段的内容进行排序。

`<?xml version="1.0" encoding="UTF-8"?>
<LIST>
    <S_ISA>
        <D_I01>00</D_I01>
        <D_I02/>
        <D_I03>00</D_I03>
        <D_I04/>
        <D_I05>01</D_I05>
        <D_I06></D_I06>
        <D_I05_2>ZZ</D_I05_2>
        <D_I07></D_I07>
        <D_I08>160427</D_I08>
        <D_I09>1106</D_I09>
        <D_I10>U</D_I10>
        <D_I11>00401</D_I11>
        <D_I12>000000001</D_I12>
        <D_I13>0</D_I13>
        <D_I14/>
        <D_I15>&gt;</D_I15>
        <S_GS>
            <D_479>SH</D_479>
            <D_142></D_142>
            <D_124></D_124>
            <D_373>20160427</D_373>
            <D_337>1106</D_337>
            <D_28>1</D_28>
            <D_455>X</D_455>
            <D_480>004010</D_480>
            <S_ST>
                <D_143>856</D_143>
                <D_329>0001</D_329>
                <S_BSN>
                    <D_353>00</D_353>
                    <D_396>0081664420</D_396>
                    <D_373>20160426</D_373>
                    <D_337>1347</D_337>
                    <D_1005>0001</D_1005>
                </S_BSN>
                <G_SHL>
                    <S_HL>
                        <D_628>0000001</D_628>
                        <D_734>0000000</D_734>
                        <D_735>S</D_735>
                        <S_PRF>
                            <D_324>SITT10-1447195769627</D_324>
                            <D_328>SITT10-1447195769627</D_328>
                        </S_PRF>
                        <S_MEA>
                            <D_737>PD</D_737>
                            <D_738>G</D_738>
                            <D_739>0.081</D_739>
                            <C_C001>
                                <D_355>KG</D_355>
                            </C_C001>
                        </S_MEA>
                        <S_TD1>
                            <D_103>PKG</D_103>
                            <D_80>00002</D_80>
                            <D_187>G</D_187>
                            <D_81>00000.18</D_81>
                            <D_355>LB</D_355>
                        </S_TD1>
                        <S_TD5>
                            <D_133>O</D_133>
                            <D_66>2</D_66>
                            <D_368>CC</D_368>
                        </S_TD5>
                        <S_REF>
                            <D_128>WH</D_128>
                            <D_127>WH:</D_127>
                        </S_REF>
                        <S_REF>
                            <D_128>RN</D_128>
                            <D_127>RN:</D_127>
                        </S_REF>
                        <S_DTM>
                            <D_374>011</D_374>
                            <D_373>20160414</D_373>
                        </S_DTM>
                        <G_SN1>
                            <S_N1>
                                <D_98>ST</D_98>
                                <D_93>C3333</D_93>
                                <S_N3>
                                    <D_166>TEST</D_166>
                                </S_N3>
                                <S_N4>
                                    <D_19>TEST</D_19>
                                    <D_116>12345</D_116>
                                    <D_26>US</D_26>
                                </S_N4>
                            </S_N1>
                        </G_SN1>
                    </S_HL>
                </G_SHL>
                <G_SHL>
                    <S_HL>
                        <D_628>0000002</D_628>
                        <D_734>0000001</D_734>
                        <D_735>O</D_735>
                        <S_MAN>
                            <D_88>CP</D_88>
                            <D_87></D_87>
                        </S_MAN>
                    </S_HL>
                </G_SHL>
                <G_SHL>
                    <S_HL>
                        <D_628>0000003</D_628>
                        <D_734>0000002</D_734>
                        <D_735>P</D_735>
                    </S_HL>
                </G_SHL>
                <G_SHL>
                    <S_HL>
                        <D_628>0000005</D_628>
                        <D_734>0000002</D_734>
                        <D_735>P</D_735>
                    </S_HL>
                </G_SHL>
                <G_SHL>
                    <S_HL>
                        <D_628>0000004</D_628>
                        <D_734>0000003</D_734>
                        <D_735>I</D_735>
                        <S_LIN>
                            <D_350>000010</D_350>
                            <D_235>SK</D_235>
                            <D_234></D_234>
                            <D_235_2>BP</D_235_2>
                            <D_234_2></D_234_2>
                        </S_LIN>
                        <S_SN1>
                            <D_350></D_350>
                            <D_382>2.000</D_382>
                            <D_355>EA</D_355>
                            <D_668>AC</D_668>
                        </S_SN1>
                    </S_HL>
                </G_SHL>
                <G_SHL>
                    <S_HL>
                        <D_628>0000006</D_628>
                        <D_734>0000005</D_734>
                        <D_735>I</D_735>
                        <S_LIN>
                            <D_350>000020</D_350>
                            <D_235></D_235>
                            <D_234></D_234>
                            <D_235_2>BP</D_235_2>
                            <D_234_2></D_234_2>
                        </S_LIN>
                        <S_SN1>
                            <D_382>1.000</D_382>
                            <D_355>EA</D_355>
                            <D_668>AC</D_668>
                        </S_SN1>
                    </S_HL>
                </G_SHL>
                <S_CTT>
                    <D_354>1</D_354>
                </S_CTT>
                <S_SE>
                    <D_96>[]</D_96>
                    <D_329>0001</D_329>
                </S_SE>
            </S_ST>
            <S_GE>
                <D_97>0</D_97>
                <D_28>1</D_28>
            </S_GE>
        </S_GS>
        <S_IEA>
            <D_I16>01</D_I16>
            <D_I12>000000001</D_I12>
        </S_IEA>
    </S_ISA>
</LIST>`

1 个答案:

答案 0 :(得分:0)

尝试

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

<xsl:template match="/LIST/S_ISA/S_GS/S_ST">
  <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:apply-templates select="G_SHL">
       <xsl:sort select="S_HL/D_628" data-type="number" order="descending"/>
     </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

我无法验证路径/LIST/S_ISA/S_GS/S_ST,但假设您在真实XML中拥有这些祖先元素。

如果您希望将兄弟元素保留在其原始输入位置,那么XSLT 2.0中的一种方法是首先使用for-each-group group-adjacent识别要排序的相邻元素:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/LIST/S_ISA/S_GS/S_ST">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each-group select="*" group-adjacent="boolean(self::G_SHL)">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <xsl:apply-templates select="current-group()">
                            <xsl:sort select="S_HL/D_628" data-type="number" order="descending"/>
                        </xsl:apply-templates>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"></xsl:apply-templates>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>