 |
| 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
|
|
|
|

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

May 17th, 2011, 08:37 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

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

May 17th, 2011, 08:46 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

May 17th, 2011, 08:57 AM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
Thanks for the reply. I tried your code and i am getting an output =
123456789....and so on in the fields
|
|

May 17th, 2011, 09:57 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

May 17th, 2011, 10:30 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|
|

May 17th, 2011, 10:32 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

May 31st, 2011, 02:13 AM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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,
|
|
 |