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 Search this Thread Display Modes
  #1 (permalink)  
Old October 11th, 2007, 12:23 PM
Authorized User
 
Join Date: Jul 2007
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to carlosaml
Default Nest elements

Hi all

I'm a noob in XSLT and I need help for doing something...

I have a XML like this:
Code:
<country name="Test">
    <page id="a">
        <banner id="1">
        </banner>
    </page>
    <page id="b">
        <banner id="1">
        </banner>
    </page>
</country>
<attribute banner_id="1" name="description">This is a test</attribute>
<attribute banner_id="1" name="url">This is an URL</attribute>
What I need to is to "nest" the <attribute>'s elements to every banner that matches the ID, and then delete all the <attribute>'s that are not nested... The resulting XML must be like this:
Code:
<country name="Test">
    <page id="a">
        <banner id="1">
            <attribute banner_id="1" name="description">This is a test</attribute>
            <attribute banner_id="1" name="url">This is an URL</attribute>
        </banner>
    </page>
    <page id="b">
        <banner id="1">
            <attribute banner_id="1" name="description">This is a test</attribute>
            <attribute banner_id="1" name="url">This is an URL</attribute>
        </banner>
    </page>
</country>
I can have the same many times spread in many different pages, but I'll have the banner attrributes only once, in the of my XML...

Thanks!
Reply With Quote
  #2 (permalink)  
Old October 11th, 2007, 01:02 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Assuming you have a surrounding document element, countries perhaps then you can use the identity template. Add an empty xsl:template for countries/attribute, this will get rid of the attribute elements, then add a template to process the banner elements:
Code:
<countries><country name="Test">
  <page id="a">
    <banner id="1">
    </banner>
  </page>
  <page id="b">
    <banner id="1">
    </banner>
  </page>
</country>
<attribute banner_id="1" name="description">This is a test</attribute>
<attribute banner_id="1" name="url">This is an URL</attribute>
</countries>


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="banner">
    <banner id="{@id}">
      <xsl:copy-of select="/countries/attribute[@banner_id = current()/@id]" />
    </banner>    
  </xsl:template>

  <xsl:template match="countries/attribute" />
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old October 11th, 2007, 01:55 PM
Authorized User
 
Join Date: Jul 2007
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to carlosaml
Default

Hmmm... Thanks... I'm trying to test this solution, but my machine is so slow, and my XML has 13MB approximately... o.O

Another thing... Is it possible to remove the banner_id from <attribute> when it is moved? After all, the <attribute> will be inside his banner... :)

Thank you.
Reply With Quote
  #4 (permalink)  
Old October 11th, 2007, 03:07 PM
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

You can speed up your transformation either

(a) by using a processor such as Saxon-SA that optimizes joins, or

(b) by optimizing them "by hand" using keys.

Where you have an expression like /a/b[c=$x], change it to key('k', $x) where the key is defined as

<xsl:key name="k" match="a/b" use="c"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old October 15th, 2007, 10:35 AM
Authorized User
 
Join Date: Jul 2007
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to carlosaml
Default

Quote:
quote:Originally posted by mhkay
 You can speed up your transformation either

(a) by using a processor such as Saxon-SA that optimizes joins, or

(b) by optimizing them "by hand" using keys.

Where you have an expression like /a/b[c=$x], change it to key('k', $x) where the key is defined as

<xsl:key name="k" match="a/b" use="c"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
I've used Saxon-SA to apply the template and it worked. (But it was soooo slow).

Now I want to remove the "banner_id" attribute from copied attributes, because they will already be inside their banner in the end...

Thanks

Reply With Quote
  #6 (permalink)  
Old October 16th, 2007, 01:53 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Instead of <xsl:copy-of select="/countries/attribute[@banner_id = current()/@id]" /> change it too:

<xsl:apply-templates select="/countries/attribute[@banner_id = current()/@id]" mode="move"/>

Then define the identify template to copy everything over, and one to stop the banner_id being copied over:
Code:
<xsl:template match="@*|node()" mode="move">
  <xsl:copy>
    <xsl:apply-templates  select="@*|node()"/>     
  </xsl:copy>
</xsl:template>
<xsl:template match="@banner_id" mode="move" priority="2"/>
/- Sam Judson : Wrox Technical Editor -/
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
HELP: I need to Nest Controls in a TreeNode ctt ASP.NET 2.0 Professional 0 January 4th, 2008 06:02 PM
nest radio button lists and postbacks lisabb ASP.NET 2.0 Basics 0 June 6th, 2007 11:41 AM
elements with same name kfir XML 8 May 1st, 2006 07:42 AM
Selecting elements up until a certain one Frode XSLT 5 January 19th, 2006 01:22 PM
Elements w same name and their attributes supafly XSLT 1 May 30th, 2005 06:34 AM



All times are GMT -4. The time now is 12:26 AM.


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