SORT XML based on Child Node text

2019-08-01 20:45发布

I have a XMLLike this

<GLLines DataTypeID="GEN" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <GLLine>
  <SYS_COMPANY_CODE>NB</SYS_COMPANY_CODE> 
  <ACCT_DOC_NUM>12720829</ACCT_DOC_NUM> 
  <ACCT_DOC_LINE>22114739</ACCT_DOC_LINE> 
  </GLLine>
   <GLLine>
  <SYS_COMPANY_CODE>NB</SYS_COMPANY_CODE> 
  <ACCT_DOC_NUM>12720827</ACCT_DOC_NUM> 
  <ACCT_DOC_LINE>22114740</ACCT_DOC_LINE> 
  </GLLine>
   <GLLine>
  <SYS_COMPANY_CODE>NB</SYS_COMPANY_CODE> 
  <ACCT_DOC_NUM>12720830</ACCT_DOC_NUM> 
  <ACCT_DOC_LINE>22114739</ACCT_DOC_LINE> 
  </GLLine>
   <GLLine>
  <SYS_COMPANY_CODE>NB</SYS_COMPANY_CODE> 
  <ACCT_DOC_NUM>12720830</ACCT_DOC_NUM> 
  <ACCT_DOC_LINE>22114738</ACCT_DOC_LINE> 
  </GLLine>
</GLLines>

I want to sort according to the acct DOC NUM and acct doc line .I tried XSLT but I don't know how to do it .so I'm getting wrong answer

标签: xml sorting xslt
2条回答
倾城 Initia
2楼-- · 2019-08-01 21:25

Start with an identity transformation that just copies the input to output unchanged.

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

Now you can add a specific template for the GLLines element to sort its children

<xsl:template match="GLLines">
  <xsl:copy>
    <xsl:apply-templates select="GLLine">
      <!-- sort first on the ACCT_DOC_NUM -->
      <xsl:sort select="ACCT_DOC_NUM" data-type="number" order="ascending" />
      <!-- for lines with the same doc num, sort by line -->
      <xsl:sort select="ACCT_DOC_LINE" data-type="number" order="ascending" />
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
查看更多
霸刀☆藐视天下
3楼-- · 2019-08-01 21:35

Try this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
  <xsl:template match="@*|node()">  
    <xsl:copy>
      <xsl:apply-templates select="@*|node()">
        <xsl:sort select="ACCT_DOC_NUM"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
查看更多
登录 后发表回答