|
 |
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 .
|
 |
|
|
 |

February 11th, 2019, 12:05 PM
|
Registered User
|
|
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 11th, 2019, 12:08 PM
|
 |
Wrox Author
Points: 18,438, Level: 59 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
|
|
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
|

February 11th, 2019, 12:15 PM
|
Registered User
|
|
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|

February 11th, 2019, 12:34 PM
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 Posts
|
|
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
|

February 11th, 2019, 05:29 PM
|
Registered User
|
|
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 11th, 2019, 05:44 PM
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 Posts
|
|
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
|

February 11th, 2019, 05:50 PM
|
Registered User
|
|
Join Date: Feb 2019
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |