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 June 2nd, 2014, 09:53 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default [XSLT] on the fly copying id value as idref

Hallo everyone,

My OUTPUT XML should look like this:

Code:
<ContextInstance id="00001">
    <Person id="1">
        <Name>Sandra Jones</Name>
        <Seks>Female</Seks>
    </Person>
    <Address id="2" refid="1">
        <Street>My Street</Street>
        <Country>USA</Country>
    </Address>
</ContextInstance>
I have been trying to create the xsl, which can reference the Attribute "refid" in the element "Addresse". Unfortunately I was not successfull. This is my actual xsl:

Code:
<xsl:template name="random_id">
    <xsl:value-of select="format-number(round(9999 * java:nextDouble($random)), '0000')" />
</xsl:template>

<ContextInstance><xsl:attribute name="id"><xsl:call-template name="random_id" /></xsl:attribute>
    
    <Person><xsl:attribute name="id"><xsl:call-template name="random_id" /></xsl:attribute>
            <Name>my XPath Expression</Name>
            <Seks>my XPath Expression</Seks>
    </Person>
    
    <Address><xsl:attribute name="id"><xsl:call-template name="random_id" /></xsl:attribute>
            <xsl:attribute name="refNB"></xsl:attribute>
            <Street>my XPath Expression</Street>
            <Country>my XPath Expression</Country>
    </Address>
    
</ContextInstance>
As you can see, I am facing the Problem to get the value of refid on the fly. The Attribute refid of the element Address references the Person id Attribute.

Does anybody have any idea how I can put copy the value from the Person id Attribute into the refid of the addresse?

Thank you very much.

Regards,

Ratna
 
Old June 2nd, 2014, 10:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Just generate it in a variable:

Code:
<ContextInstance><xsl:attribute name="id"><xsl:call-template name="random_id" /></xsl:attribute>
 
    <xsl:variable name="pid">
      <xsl:call-template name="random_id" />
    </xsl:variable>   
    <Person><xsl:attribute name="id"><xsl:value-of select="$pid" /></xsl:attribute>
            <Name>my XPath Expression</Name>
            <Seks>my XPath Expression</Seks>
    </Person>
    
    <Address><xsl:attribute name="id"><xsl:call-template name="random_id" /></xsl:attribute>
            <xsl:attribute name="refid"><xsl:value-of select="$pid"/></xsl:attribute>
            <Street>my XPath Expression</Street>
            <Country>my XPath Expression</Country>
    </Address>
    
</ContextInstance>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 4th, 2014, 11:36 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much, Mr Kay, it works.





Similar Threads
Thread Thread Starter Forum Replies Last Post
'this.ID = id;' in class construction holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 6th, 2006 10:58 AM
How to resolve IDREF in XSL teahouse XSLT 3 September 15th, 2006 01:14 PM
XSLT change class attribute by ID? matallen XSLT 8 March 1st, 2006 05:00 PM
generate id grouping using xslt meetnaveen4u XSLT 0 May 23rd, 2005 05:14 AM
why not index.asp?id=1 can be www.myweb.com/?id=1 BurhanKhan Classic ASP Professional 11 September 6th, 2004 02:06 PM





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