Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 11th, 2019, 12:05 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #2 (permalink)  
Old February 11th, 2019, 12:08 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old February 11th, 2019, 12:15 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>
Reply With Quote
  #4 (permalink)  
Old February 11th, 2019, 12:34 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #5 (permalink)  
Old February 11th, 2019, 05:29 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #6 (permalink)  
Old February 11th, 2019, 05:44 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #7 (permalink)  
Old February 11th, 2019, 05:50 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up 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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a XML file itself inieto XSLT 8 February 2nd, 2011 05:03 PM
HTML portion of aspx file SKhna ASP.NET 2.0 Basics 3 March 25th, 2008 10:52 AM
Extracting portion of XML ** Need help asap** aintvoguish XSLT 3 December 20th, 2007 09:12 AM
Sort xml file victorcorey XSLT 5 December 4th, 2007 01:40 PM
Sort XML in dropdownlist Hughesie78 XSLT 1 November 23rd, 2007 08:41 AM



All times are GMT -4. The time now is 04:10 AM.


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