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 November 8th, 2007, 10:05 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to add an attribute to each element

Hi All!

I have been reading this forum and I am impressed your knowledge!

I couldn't find an advice how to solve my problem. I am going to construct a stylesheet which can add an attribute to each element, let me consider a successive positive integer, to (what is important!) any xml document!

This is what I was created..

<xsl:template match="/">
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="*">
<xsl:for-each select="//*">
    <xsl:copy>
      <xsl:attribute name="pos">
          <xsl:number count="*" level="any" />
      </xsl:attribute>
  </xsl:copy>
</xsl:for-each>
</xsl:template>

This stylesheet returns stuff like this:
<n1 pos="1"/>
<n2 pos="2"/>
<n3 pos="3"/>
<n4 pos="4"/>
<n5 pos="5"/>

and I need a one which could maintain the structure of source document and returns something like this:
<n1 pos="1">
<n2 pos="2">
<n3 pos="3"></n3>
<n4 pos="4"></n4>
</n2>
<n5 pos="5"></n5>
</n1>


Of course to this example the source looks like:
<n1>
<n2>flaflafla
<n3>content3</n3>
<n4>lelele</n4>
</n2>
<n5></n5>
</n1>
but it could be totally different!

Any help would be great!

bart

Reply With Quote
  #2 (permalink)  
Old November 8th, 2007, 10:17 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

Its always best in these case to start with the identity template and adapt it to suit your needs.

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

We actually want to treat attributes and nodes differently, so we'll split the identity template into two:

<xsl:template match="@*">
  <xsl:copy/>
</xsl:template>

<xsl:template match="node()">
  <xsl:copy>
    <xsl:attribute name="pos">
      <xsl:number count="*" level="any"/>
    </xsl:attribute>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>


/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old November 8th, 2007, 10:42 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much Sam, now world seems to be a little bit easier, because from one side is solving problems and from the other finding the correct way of analyse the case.

I just want to add that that I needed to divide your stylesheet into two parts: one with the identity and second with the rest (started with <xsl:import /> of course), because I received an errors/warnings:
(using saxon and xalan as well)
Error
  XTRE0540: Ambiguous rule match for /term
Matches both "node()" on line 11 of file:/D:/xslt/n2_num.xsl
and "node()" on line 32 of file:/D:/xslt/n2_num.xsl
but the correct result was displayed.

After dividing everything works perfect. Real appreciate!
Reply With Quote
  #4 (permalink)  
Old November 8th, 2007, 12:12 PM
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

Err... you weren't meant to include both bits of code, just the bottom two templates. The first one was simply meant to illustrate how I started out.

/- 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
Attribute to Child element Conversion kudzuconf XSLT 6 July 13th, 2007 04:02 AM
How to add attribute to unbounded element 2BOrNot2B XML 4 January 16th, 2007 03:42 PM
value-of within the HTML element attribute BrendonMelville XSLT 1 March 14th, 2006 11:32 AM
Attribute for first element only mehdi62b XML 1 January 2nd, 2006 06:26 AM
How can I get the element node of an attribute cdias XSLT 2 March 15th, 2004 10:34 AM



All times are GMT -4. The time now is 02:21 PM.


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