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 August 12th, 2008, 09:23 AM
Authorized User
 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl recursion problem

In the following xsl, I'm trying to create a record typically of the form (name:value). I need to add a unique id for this record. So every where (name:'<xsl:value-of select="@name") is seen in the current version, I need to add (id:value, name:<xsl:value-of select="@name"). The value of id should be unique. Any help??


<xsl:template match="eSubpackages">
    { label: 'name',
identifier: 'name',
items:[

<xsl:for-each select="eClassifiers">
<xsl:choose>
<xsl:when test="position()!=last()">


{name:'<xsl:value-of select="@name"/>',

<xsl:if test= "(child::node())">
children:[
<xsl:for-each select="child::node()[local-name()][@name]">

<xsl:choose>
<xsl:when test="position()!=last()">
{name:'<xsl:value-of select= "@name" />'},


</xsl:when>
<xsl:otherwise>
{name:'<xsl:value-of select= "@name" />'}
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
]
</xsl:if>
},
</xsl:when>


<xsl:otherwise>

{name:'<xsl:value-of select="@name"/>',

<xsl:if test= "(child::node())">
children:[
<xsl:for-each select="child::node()[local-name()][@name]">

<xsl:choose>
<xsl:when test="position()!=last()">
{name:'<xsl:value-of select= "@name" />'},


</xsl:when>
<xsl:otherwise>
{name:'<xsl:value-of select= "@name" />'}
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
]
</xsl:if>
}
</xsl:otherwise>
</xsl:choose>


</xsl:for-each>



]
}
</xsl:template>

</xsl:stylesheet>

 
Old August 12th, 2008, 09:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Depending on the format of the ID you want you could look at either generate-id() function, or the <xsl:number> instruction.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 14th, 2008, 12:43 AM
Authorized User
 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Sam! That was an useful tip! However, I ran into another problem.

The input on which this xsl works is an xml schema (rather than an xml data object)and am trying to render that info in the form of a tree . Since am using xml schema, I have some child elements of a node which can be of user defined types. I need to expand these child nodes but then no child nodes are present as such in the input xml schema. Those user defined types might be present as separate elements. Hence, I have to match the user defined type of the child nodes with the types of separate elements (to add the matched element's child nodes under the child node of an element which has a user defined type).

 As you might see, using generate-id() gives the same id as it is node based. So, distinct value condition for id gets violated. Any suggestions?/

 
Old August 14th, 2008, 03:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well looks like <xsl:number> might be a better solution then.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 14th, 2008, 05:46 AM
Authorized User
 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Would it be possible to give a bit more detail on how this solves the unique id problem?

Here is some detail on the problem


<types>

<process>
<process step>
<gap kind="GAP"/>
</process step>
</process>

<Gap eType="GAP">
<name/>
<id/>
</types>

I need the child elements of Gap to be displayed under process->process step->gap since the nested gap element matches with some other type Gap(shown as a separate element). So the problem is how to assign different id's for an element when there is a change in the context?

 
Old August 14th, 2008, 07:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well <xsl:number> will (by default) generate a new number each time it is called - much like a counter might. This means that if you use a referenced user type it will have a new number, rather than generate-id() which will as you say generate the same id for the user type each time it is used.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursion problem dani1 XSLT 5 November 26th, 2008 10:05 AM
recursion in XSL jekkos XSLT 8 January 2nd, 2007 06:16 PM
Recursion lincsimp XSLT 1 August 16th, 2005 04:26 PM
recursion yui0329 C# 6 April 28th, 2005 09:36 AM
recursion nulogix PHP How-To 1 June 28th, 2004 03:58 PM





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