p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Sort portion of XML File (http://p2p.wrox.com/showthread.php?t=100156)

pedromcerveirapinto February 11th, 2019 12:05 PM

Sort portion of XML File
 
Good day,
I'm new to the forum and to XSLT Transformations. I've read a similar post to mine but found no answer.
I have a rather large XML file which I need sorted in some blocks.
The XML structure file is:
<AuditFile>
<GeneralLedgerEntries>
<Journal>
<Transaction>
<Lines>
<DebitLine>
</DebitLine>
<CreditLine>
</CreditLine>
</Lines>
</Transaction>
</Journal>
</GeneralLedgerEntries>
</AuditFile>

Where inside Lines we can have any number of CreditLines and/or DebitLine and in whatever order, that is we can have them interchanging.
I need this Lines block sorted so that I have all CreditLines and only after that all DebitLines.
Furthermore this Lines block repeats along the file.
Could you please help me?

My poor attempt at getting this done was as follows:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<!-- Sort Lines -->
<xsl:template match="Lines">
<Lines>
<xsl:apply-templates>
<xsl:sort select="name()"/>
</xsl:apply-templates>
</Lines>

</xsl:template>

Hope you can help me.
Thanks.

mhkay February 11th, 2019 12:08 PM

You don't need a sort here, just do

Code:

<xsl:template match="Lines">
  <Lines>
    <xsl:apply-templates select="CreditLines"/>
    <xsl:apply-templates select="DebitLines"/>
  </Lines>
</xsl:template>


pedromcerveirapinto February 11th, 2019 12:15 PM

Sort portion of XML File
 
Thank you so much for the awesome quick reply.
Unfortunately, it did not work.
I still have this bit as outcome:
Code:

<Lines>
              <CreditLine>
                  <RecordID>001</RecordID>
                  <AccountID>2312101</AccountID>
                  <SystemEntryDate>2018-09-21T18:36:31</SystemEntryDate>
                  <Description>Vencimento201808</Description>
                  <CreditAmount>4904.26</CreditAmount>
              </CreditLine>
              <DebitLine>
                  <RecordID>002</RecordID>
                  <AccountID>2312101</AccountID>
                  <SystemEntryDate>2018-09-21T18:36:31</SystemEntryDate>
                  <Description>Vencimento201808</Description>
                  <DebitAmount>1894.68</DebitAmount>
              </DebitLine>
              <CreditLine>
                  <RecordID>003</RecordID>
                  <AccountID>2420101</AccountID>
                  <SystemEntryDate>2018-09-21T18:36:31</SystemEntryDate>
                  <Description>Vencimento201808</Description>
                  <CreditAmount>1249.0</CreditAmount>
              </CreditLine>
          </Lines>

Resulting XSLT file is now:
Code:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
     
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- Sort Lines -->
<xsl:template match="Lines">
  <Lines>
    <xsl:apply-templates select="CreditLine"/>
    <xsl:apply-templates select="DebitLine"/>
  </Lines>
</xsl:template>
     
</xsl:stylesheet>


Martin Honnen February 11th, 2019 12:34 PM

The suggestion of Mike works fine for me at https://xsltfiddle.liberty-development.net/ej9EGbX, I suppose your real XML has some namespaces you have not shown so that the template Mike has suggested is never applied. Please post minimal but complete samples of XML, XSLT, result you get plus the one you want together with an explanation of how you run the XSLT.

pedromcerveirapinto February 11th, 2019 05:29 PM

Sort portion of XML File
 
Good day,
Again, thank you so much for the fast reply.

I'm actually using that website for XSLT experiments but I missed the save feature.

Here's the full XML (or a complete sample at least):

https://xsltfiddle.liberty-development.net/jyRYYi2

Hope to year from you soon,
Thanks again.

Martin Honnen February 11th, 2019 05:44 PM

If you use an XSLT 1 processor then, as the input elements are in a namespace, in the XSLT you need to declare that and use it to qualify element names:
Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:df="urn:OECD:StandardAuditFile-Tax:PT_1.04_01">
...
<xsl:template match="df:Lines">
  <xsl:copy>
    <xsl:apply-templates select="df:CreditLine"/>
    <xsl:apply-templates select="df:DebitLine"/>
  </xsl:copy>
</xsl:template>

If you can use an XSLT 2 or 3 processor like Saxon 9 you can leave the template but simply need to add the attribute
Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="urn:OECD:StandardAuditFile-Tax:PT_1.04_01">
Working with XML documents using namespaces is probably the FAQ of XSLT/XPath so make sure you get your hands on a book or tutorial, Mike in his book for sure covers it.

pedromcerveirapinto February 11th, 2019 05:50 PM

Sort portion of XML File
 
Many many thanks. It worked with the namespaces.
Now I just need a second transformation that removes namespaces.
Thanks again. Top notch.


All times are GMT -4. The time now is 05:25 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.