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 October 11th, 2007, 12:23 PM
Authorized User
 
Join Date: Jul 2007
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!
 
Old October 11th, 2007, 01:02 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old October 11th, 2007, 01:55 PM
Authorized User
 
Join Date: Jul 2007
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.
 
Old October 11th, 2007, 03:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old October 15th, 2007, 10:35 AM
Authorized User
 
Join Date: Jul 2007
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

 
Old October 16th, 2007, 01:53 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/





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





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