Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
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 Display Modes
  #1 (permalink)  
Old September 8th, 2008, 04:32 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default Problem adding element to the previous element

Hi!

Here is my problem.

I need to convert XML to XML with different structure.
I have the flat structure and i need to build the hierarchy. For example:

<LIST>
<Invoice>
<InvoiceNum>1</InvoiceNum>
<Delivery>
<DeliveryNum>22<DeliveryNum>
</Delivery>
</Invoice>
<Invoice>
<InvoiceNum>3</InvoiceNum>
<Delivery>
<DeliveryNum>444<DeliveryNum>
</Delivery>
</Invoice>
<Invoice>
</InvoiceNum>
<Delivery>
<DeliveryNum>22<DeliveryNum>
</Delivery>
</Invoice>
</LIST>


If "InvoiceNum" is empty, the "Dlivery" belongs to the previous "Invoice".

How can I add the Delivery to the previous element (Invoice)?

The structure must have this hierarchy:

<LIST>
<Invoice>
<InvoiceNum>1</InvoiceNum>
<Delivery>
<DeliveryNum>22<DeliveryNum>
</Delivery>
</Invoice>
<Invoice>
<InvoiceNum>3</InvoiceNum>
<Delivery>
<DeliveryNum>444<DeliveryNum>
</Delivery>
<Delivery>
<DeliveryNum>22<DeliveryNum>
</Delivery>
</Invoice>
</LIST>





Thanks for help!


Reply With Quote
  #2 (permalink)  
Old September 8th, 2008, 04:51 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

I assume that in your example input, </InvoiceNum> is a typo for <InvoiceNum/>.

In XSLT 2.0

<xsl:template match="LIST">
  <xsl:for-each-group group-starting-with="Invoice[InvoiceNum != '']">
    <Invoice>
      <xsl:copy-of select="InvoiceNum"/>
      <xsl:copy-of select="current-group()/Delivery"/>
    </Invoice>
  </
</

In 1.0 (in case you're stuck with it) use sibling recursion. That is, apply-templates to Invoice[1], then in the template for Invoice (a) do an apply-templates to following-sibling::Invoice[1] (in a different mode) provided it has no InvoiceNum, and (b) do an apply-templates (in the original mode) to the first following-sibling::Invoice that does have an InvoiceNum.


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old September 8th, 2008, 07:52 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you Michael!

I'll try to use your example.

Reply With Quote
  #4 (permalink)  
Old September 9th, 2008, 02:26 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 I assume that in your example input, </InvoiceNum> is a typo for <InvoiceNum/>.

In XSLT 2.0

<xsl:template match="LIST">
††<xsl:for-each-group group-starting-with="Invoice[InvoiceNum != '']">
    <Invoice>
    ††<xsl:copy-of select="InvoiceNum"/>
    ††<xsl:copy-of select="current-group()/Delivery"/>
    </Invoice>
††</
</

In 1.0 (in case you're stuck with it) use sibling recursion. That is, apply-templates to Invoice[1], then in the template for Invoice (a) do an apply-templates to following-sibling::Invoice[1] (in a different mode) provided it has no InvoiceNum, and (b) do an apply-templates (in the original mode) to the first following-sibling::Invoice that does have an InvoiceNum.


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference

I tried to use this code:

<xsl:for-each-group group-starting-with="Invoice[InvoiceNum != '']">
                <Invoice>
                    <xsl:copy-of select="InvoiceNum"/>
                    <xsl:copy-of select="current-group()/Delivery"/>
                </Invoice>
            </xsl:for-each-group>

But I've got the error message: Function current-group does not exist :( What's wrong?
I'am using the Stylus studio 2008.
Reply With Quote
  #5 (permalink)  
Old September 9th, 2008, 04:11 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

When using Stylus Studio, check that you have the options set to request an XSLT 2.0 processor (I think Saxon is the only one they support). The error message looks like it comes from a 1.0 processor.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old September 10th, 2008, 01:38 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 When using Stylus Studio, check that you have the options set to request an XSLT 2.0 processor (I think Saxon is the only one they support). The error message looks like it comes from a 1.0 processor.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Thanks Michael!

But it didn't help :(
Reply With Quote
Reply


Thread Tools
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
Problem in adding the new element into the XML fil Somesh C# 3 February 6th, 2007 05:41 AM
adding options to a select element dymanicly StevesonD Javascript How-To 0 September 28th, 2006 09:53 AM
Adding a new Element monuindia2002 XML 2 March 13th, 2006 10:59 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 06:04 PM
Adding an element to a binded DropDownlist mahulda ASP.NET 1.0 and 1.1 Basics 3 March 10th, 2004 02:12 PM



All times are GMT -4. The time now is 12:54 PM.


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