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 November 8th, 2007, 10:05 AM
Registered User
 
Join Date: Nov 2007
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

 
Old November 8th, 2007, 10:17 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old November 8th, 2007, 10:42 AM
Registered User
 
Join Date: Nov 2007
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!
 
Old November 8th, 2007, 12:12 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/





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





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