SORT XML based on Child Node text

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

Answers


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>

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>

Need Your Help

Creating function in r

r function

If I have data set named 'example' and it has 'id'variable.