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 17th, 2011, 08:32 AM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML numbering

Hi,

I have following XML where some nodes have a number assigned to them. What I am trying to achieve is put some logic to assign a number to the remaining nodes. Which means i want to assign 4 to the node after that has number 3. Can anyone help how to achieve this?

<item>1<item>
<item>2<item>
<item>3<item>
<item><item>
<Item><item>
<item><item>

Thanks,
 
Old May 17th, 2011, 08:37 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well you haven't really explained whether the preceding "item" elements can have any number or whether they are simply numbered sequentially (i.e. 1,2,3,4,...).
If they are numbered sequentially and all "item" elements are sibling nodes then doing
Code:
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="item[not(node())]">
  <xsl:copy>
    <xsl:number/>
  </xsl:copy>
</xsl:template>
should suffice.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old May 17th, 2011, 08:42 AM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thank you for the reply. the preceding elements are all numbered sequentially. And I cannot use xsl:copy because of the way the XSLT is set up. I would like to achive this from the context of the current node.

For eg I have:

<xsl:template match=item >

<!--get the logic here to obtain its position-->
</xsl:template>
 
Old May 17th, 2011, 08:46 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well in my previous suggestion the number you are looking for is output by "<xsl:number/>", not by xsl:copy.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old May 17th, 2011, 08:57 AM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thanks for the reply. I tried your code and i am getting an output =

123456789....and so on in the fields
 
Old May 17th, 2011, 09:57 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Please show us exactly what you do.
When I edit your input sample to get a well-formed XML document
Code:
<items>
<item>1</item>
<item>2</item>
<item>3</item>
<item></item>
<item></item>
<item></item>
</items>
and then use my code in the following complete stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="item[not(node())]">
  <xsl:copy>
    <xsl:number/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Saxon 6.5.5 outputs the following result:
Code:
<?xml version="1.0" encoding="utf-8"?><items>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
</items>
Now you said you don't want to use xsl:copy but so your code might be slightly different but if you want us to help fix it then please show us the complete code.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old May 17th, 2011, 10:30 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>i am getting an output 123456789....

Is that good or bad? You don't say.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 17th, 2011, 10:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

PS: I can't imagine what impression you intend to give people by your choice of nickname.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 31st, 2011, 02:13 AM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin and mhkay,

Thank you for your help. Sorry it was my mistake I was doing something wrong. Your solution worked for me.

Thanks again for your help,





Similar Threads
Thread Thread Starter Forum Replies Last Post
Numbering - For each Navy1991_1 XSLT 3 July 2nd, 2008 07:56 AM
Numbering in XSLT Angel1221 XSLT 1 June 10th, 2008 05:07 PM
Numbering problem EstherMStrom XSLT 0 January 21st, 2005 03:21 PM
numbering harag XSLT 2 November 6th, 2003 06:04 AM
XSL numbering be782524 XSLT 2 August 5th, 2003 05:14 AM





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