Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2013, 02:09 PM
Authorized User
 
Join Date: Apr 2006
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Post Guidance for complex XSLT

Hi - I'm working on parsing XML that is to be converted to text and submitted to FinCEN, who requires it to be in a very specific format. Basically, I will get an xml file with elements all arranged as siblings and their association with each other is based on order of appearance in the file. So I may have a file that looks like this:

<FilingInstitution> (2A)

<FinancialInstitution> (2B)
<SuspiciousActivityInformation> (3A)
<SubjectInformation> (4A)

<FinancialInstitution> (2B)
<SuspiciousActivityInformation> (3A)
<SubjectInformation> (4A)
<SubjectInformation> (4A)
<SubjectInformation> (4A)

Basically, the pseudo-code would look like this:



Output 1A
For each 2A
Output 2A
For each 2B before the next 2A
Output 2B
For each 3A before the next 2B
Output 3A; increment counter
For each 4A before the next 3A
Output 4A with above counter

The text will essentially need to be formatted in the order it is in the file, with some summary sections (after each 2B, for instance) . I'm having trouble figuring out how to loop over elements that follow a 3A, for example without looping over all the 4A records in the document. For each 3A, I will also need to keep a counter so I can output that with the text for the 3A section and associated 4A sections.

Originally, I had designed a series of <xsl:for-each> loops that would go through each of elements in the file and call the appropriate once for each, but I realized that will pick out all the elements in the file and not just the ones that appear beneath the relevant (parent) element that I want to see for that section.

Then, I considered using a catch-all <xsl:apply-templates/> so all the elements would be dealt with in the order they appear in the file. However, I don't see how I could use any kind of counter if I do it this way. (I understand I can't use an actual counter, but my contention was to call a template inside a for each loop and pass the position() as a parameter)

Please let me know if you have any suggestions on where to go with this. I have some XSL experience, but this is a bit different than what I'm used to.

Thanks in advance.

Last edited by Dracarnion; February 20th, 2013 at 02:21 PM.. Reason: added pseudo-code
 
Old February 20th, 2013, 02:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0 you can probably use the positional grouping facilities for this kind of processing: that is, xsl:for-each-group typically with the group-adjacent, grouo-starting-with, or group-ending-with attributes. At first glance, group-starting-with="FinancialInstitution" would be a good start. You can always use another level of grouping to process the resulting groups.

It's harder in XSLT 1.0, at least until you get the hang of it. The general idea is called "sibling recursion": use apply-templates not to process all the children, but to process the next immediately following child, which allows you to stop processing siblings when you reach the next child.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Dracarnion (February 20th, 2013)
 
Old February 20th, 2013, 04:26 PM
Authorized User
 
Join Date: Apr 2006
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Someone had recommended using an Identity Transform. Does this seem like a good approach as well? http://en.wikipedia.org/wiki/Identity_transform

Note that I'm not copying directly but reading and manipulating data to be output.
 
Old February 20th, 2013, 04:43 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The identity transform pattern is useful where there are subtrees in the input that you want to copy with small changes to the output. You haven't described your problem in enough detail for me to judge whether that is the case. It's certainly not the essence of the problem as you've described it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 20th, 2013, 04:53 PM
Authorized User
 
Join Date: Apr 2006
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Look here for a more detailed post:

http://stackoverflow.com/questions/1...r-complex-xslt

I'm basically copying specific pieces of data from the source XML, but doing some transforming along the way to produce the output file. Please let me know if you have questions that I could answer to give you more of an idea of what I'm looking for.

Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Guidance gopichaithanya C# 6 February 9th, 2007 04:57 PM
need guidance please samanta103 Intro Programming 2 March 16th, 2006 01:08 PM
Guidance required whyvenky General .NET 0 February 26th, 2006 03:03 AM
Complex How To In XSLT Wee Bubba XSLT 2 April 8th, 2005 07:16 AM
Need guidance Newkid Classic ASP Components 4 July 27th, 2004 04:23 PM





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