Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
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 January 21st, 2007, 06:23 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default transformation from attributes to nested elements

Hi everybody,

I'm working with XSLT for a long time now, so I'm no newbie. (for the case that there is only an advanced solution then I'm able to understand it).
I'm currently working on a transformation from XML to XML based on XSLT 2.0. I got a problem, that is a little tricky. For one part of the problem there is a workaround, but for the second part I need some advice.

Say, I've got an element in my source-file that has a structure like this:

<element a1="v1" a2="v2" a3="v3" a4="v4" a5="v5" a6="v6" />

All attributes are optional!
I need a transformation that creates nested elements from the attributes. The order is not important.

So, what I need looks like this for example:

<element>
  <a1>
    <a2>
      <a3>
        <a5>
          <a6>
          </a6>
        </a5>
      </a3>
    <a2>
  </a1>
</element>

I could not find a normal way to do this with standard xslt-mechanisms because if I use a simple template that match the attributes like:
<xsl:template match="element/@*">...</xsl:template>
then I don't get nested elements. That's clear.

It does not work neither when you use a single template for each attribute like:
<xsl:template match="element/@a1">
  <xsl:apply-templates select="???"/>
</xsl:template>
because what should '???' be? (Attributes have no siblings.)
But you could use:
<xsl:template match="element/@a1">
  <xsl:variable name="attname"><xsl:value-of select="name()"/></xsl:variable>
  <xsl:apply-templates select="../@*[name()!=$attname]"/>
</xsl:template>
But with this solution you get an infinite loop.
So the only way would be to note all attributes that were processed and always make a comparison if an attribute was already processed. Am I right?

Second solution:
<xsl:template match="element">
  <xsl:if test="@a1">

  </xsl:if>
  <xsl:if test="@a2">

  </xsl:if>
  <xsl:if test="@a3">

  </xsl:if>

  <xsl:if test="@a3">

  </xsl:if>
  <xsl:if test="@a2">

  </xsl:if>
  <xsl:if test="@a1">

  </xsl:if>
</xsl:template>

But it is not possible to create a single start-tag here and an end-tag there, because the n the stylesheet is not wellformed!
Solution:
Create start-tag like this:
<xsl:text disable-output-escaping="yes">&lt;a1&gt;</xsl:text>
and create end-tag like this:
<xsl:text disable-output-escaping="yes">&lt;/a1&gt;</xsl:text>

This solution would be ok if I just want to write the output to a file.
My problem is, that I want to keep everything as real Nodesets inside a variable and work with it later.
If I do it this way I don't get Element-Nodes.

Does anyone have a good idea? Thanks in advance.

Erik





Reply With Quote
  #2 (permalink)  
Old January 21st, 2007, 06:33 PM
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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

How about:

<xsl:template match="element">
  <xsl:call-template name="att">
    <xsl:with-param name="atts" select="@*"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template name="att">
  <xsl:param name="atts"/>
  <xsl:if test="$atts">
    <xsl:element name="{$atts[1]/name()}">
      <xsl:call-template name="att">
        <xsl:with-param name="atts" select="$atts[position() != 1]"/>
      </
    </
  </
</

As it happens, this is pure XSLT 1.0.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old January 21st, 2007, 07:21 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Sometimes you one can't see the wood for the trees"

Now I'm a little embarrassed. So simple.
Thanx a lot for your reply, Michael Kay.
Another question:
I'm interested in approved xslt-patterns (similar to GOF-Patterns) concerning performance, standard-problems ...
(Your solution could be the pattern: "Transforming Attributes to Elements")
Is somebody working on this or is anybody interested in participating?

Erik
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
Default attributes from DTD while transformation nmahesh567 XSLT 5 March 28th, 2007 03:31 AM
sequenced elements to attributes zkent XSLT 0 April 19th, 2006 08:50 AM
Nested XML to CSV transformation 2Poc XSLT 2 September 19th, 2005 03:11 AM
Elements and attributes Morrislgn XSLT 1 June 20th, 2005 11:13 AM
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 06:02 PM.


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