Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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
  #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!


  #2 (permalink)  
Old September 8th, 2008, 04:51 AM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
  #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.

  #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.
  #5 (permalink)  
Old September 9th, 2008, 04:11 AM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
  #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 :(


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 06: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 11:59 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM
Adding an element to a binded DropDownlist mahulda ASP.NET 1.0 and 1.1 Basics 3 March 10th, 2004 03:12 PM





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