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 May 9th, 2007, 04:03 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem nested elements

Hi!

I am usigng XSLT to transform a XML-File to another XML-File. My problem is the nested structure of the elements. In the output file i want to keep the nested structure.

I am new to XSLT and i tried it with named templates, but i didn't solved it.

The element "item" can be nested to a arbitrarily depth. Only the last item can have the attribute src.

The input file looks like this:

<root>
<item title="a">
  <item title="a">
    <item title="a" src="xy"/>
  </item>
</item>
<item title="a" src="xy"/>
<item>
  <item title="a" src="xy"/>
</item>
<othertag/>
</root>

Is it possible to solve this with XSLT 1.0, how?
Thanks for every hint!


 
Old May 9th, 2007, 04:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't actually say what output you want!

Usually this kind of transformation uses an identity template rule:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

which on its own will produce output that is identical to the input. You then typically add further template rules to describe the exceptions, that is elements that should not be copied exactly but modified in some way.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 9th, 2007, 04:42 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oh sorry. the output has basically the same structure as the input, but some elements are added (always "addedElement1", excepting the element "item" has no child, then "addedElement2") and the element names are different.


so for example:

<root>
<module title="a">
  <addedElement1/>
  <module title="a">
    <addedElement1/>
    <module title="a" src="xy">
      <addedElement2/>
    </module>
  </module>
</module>
<module title="a" src="xy">
  <addedElement2/>
</module>
<module>
  <addedElement1/>
  <module title="a" src="xy">
    <addedElement2/>
  </module>
</module>
<othertag/>
</root>

 
Old May 9th, 2007, 04:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

So you need a couple of rules like

<xsl:template match="item">
  <module title="{@title}">
    <addedElement1/>
    <xsl:apply-templates/>
  </module>
</xsl:template>

<xsl:template match="item[@src]">
  <module title="{@title}" src="{@src}">
    <addedElement2/>
    <xsl:apply-templates/>
  </module>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 9th, 2007, 07:00 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But how do i get the nested structure of the elements? I will need a recursive template call?? Or not?

 
Old May 9th, 2007, 07:06 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Did you try the transformation Michael proposed? It is recursive.

--

Joe (Microsoft MVP - XML)
 
Old May 9th, 2007, 07:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, you need a recursive template call. That's what xsl:apply-templates does.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 9th, 2007, 07:27 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I didn't tried it because i missed the root element and thought it couldn't be recursive!
But it works! Thx! Now i understand XSLT a little bit more. Before i always used \\item in the match Attribute of the Template to get all items. But this is completely wrong.

Here is the whole XSLT ... if some newbie also needs it ;)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>

<xsl:template match="item">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="item">
  <module title="{@title}">
    <addedElement1/>
    <xsl:apply-templates/>
  </module>
</xsl:template>

<xsl:template match="item[@src]">
  <module title="{@title}" src="{@src}">
    <addedElement2/>
    <xsl:apply-templates/>
  </module>
</xsl:template>
</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding nested elements XSLT 1? Budbertzerofluff XSLT 2 November 15th, 2008 02:45 PM
transformation from attributes to nested elements e-bell XSLT 2 January 21st, 2007 07:21 PM
path for nested elements rjonk XSLT 7 November 20th, 2006 05:43 AM
sorting nested elements again stekker XSLT 1 June 5th, 2006 03:38 AM
sorting nested elements stekker XSLT 5 June 5th, 2006 01:19 AM





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