Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 27th, 2006, 06:09 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl advise needed - Replacing UID through XSL

Hi,

I have a need to replace all GUID (or UID consist of 32 character) in an installation project file (xml format). I wrote a xsl which replaces all uid elements with the one I specify in <uid>. Since UID must be unique, I must come up a way to insert individual UID. I can generate the UID in Java code. I am thinking (not sure it's possible) two ways of handling this: one is to generate a list of UID in an xml file, and import each UID to each <uid> element. Second is to invoke the function that returns the UID directly in xsl.

I am new to xsl, so I appreciate your help in this issue. Thanks!

Billy



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="*|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="section[@name='Product']/productTree//key/uid">
        <uid>12345678901234567890123456789012</uid>
    </xsl:template>
</xsl:stylesheet>



Reply With Quote
  #2 (permalink)  
Old February 27th, 2006, 06:17 PM
mhkay's Avatar
Wrox Author
Points: 17,770, Level: 58
Points: 17,770, Level: 58 Points: 17,770, Level: 58 Points: 17,770, Level: 58
Activity: 77%
Activity: 77% Activity: 77% Activity: 77%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,838
Thanks: 0
Thanked 267 Times in 262 Posts
Default

Both techniques are possible. Writing a file containing the mappings from old to new UIDS is probably cleaner, because calling Java extension functions is processor-dependent.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old February 27th, 2006, 06:23 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael,

Could you point me to some reference online or books that talk about the mapping technique? Or perhaps, you can provide me with a sample code? I am planning to have the guid.xml for my GUID entries:

<guids>
  <guid>12345678900987654321123456789098</guid>
  <guid>abcdec78900987654321123456789098</guid>
  <guid>123456789adeca980a00123456789098</guid>
  <guid>403ab678900987654321123456789098</guid>
  <guid>12afes78900987654321123456789098</guid>
</guilds>

How do I import this xml into the xsl, and how do I reference each guid in <gui> element? Thanks!

Billy

Reply With Quote
  #4 (permalink)  
Old February 27th, 2006, 07:02 PM
mhkay's Avatar
Wrox Author
Points: 17,770, Level: 58
Points: 17,770, Level: 58 Points: 17,770, Level: 58 Points: 17,770, Level: 58
Activity: 77%
Activity: 77% Activity: 77% Activity: 77%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,838
Thanks: 0
Thanked 267 Times in 262 Posts
Default

You use the document() function to read the XML (or you can pass it as a parameter to your stylesheet, and then you locate the elements within it using path expressions just as in your primary document.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old February 27th, 2006, 07:31 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

With the document() function, I was able to retrieve the data from an external xml file. However, It writes all UID with the first GUID. I know that somehow I need to use a loop or some reference to the next element in guid.xml, but just not sure where. could you shine some light?

Thanks!

Billy


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="*|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="section[@name='Product']/productTree//key/uid">
        <uid><xsl:value-of select="document('guid.xml')/guids/guid"/></uid>
    </xsl:template>
</xsl:stylesheet>

Reply With Quote
  #6 (permalink)  
Old February 27th, 2006, 08:46 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I wrote the following code, but still it's not going through the guid.xml in increasing order:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="*|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>


    <xsl:template name="replacement">
        <xsl:param name="n"/>
        <uid><xsl:value-of select="document('guid.xml')/guids/guid[$n]"/></uid>
    </xsl:template>


    <xsl:template match="section[@name='Product']/productTree//key">
        <xsl:param name="n" select="1"/>
        <xsl:for-each select="uid">
                <xsl:call-template name="replacement">
                    <xsl:with-param name="n" select="$n"/>
                </xsl:call-template>
                <xsl:param name="n" select="$n+1"/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>


Reply With Quote
  #7 (permalink)  
Old February 28th, 2006, 04:46 AM
mhkay's Avatar
Wrox Author
Points: 17,770, Level: 58
Points: 17,770, Level: 58 Points: 17,770, Level: 58 Points: 17,770, Level: 58
Activity: 77%
Activity: 77% Activity: 77% Activity: 77%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,838
Thanks: 0
Thanked 267 Times in 262 Posts
Default

You're getting close. This should give you a compile error:

        <xsl:param name="n" select="1"/>
        <xsl:for-each select="uid">
                <xsl:call-template name="replacement">
                    <xsl:with-param name="n" select="$n"/>
                </xsl:call-template>
                <xsl:param name="n" select="$n+1"/>
        </xsl:for-each>

because you can't use xsl:param here. (If you don't get an error, it's a bug in your XSLT processor). However, what you are trying to do is basically flawed, because XSLT, as a functional programming language, doesn't have updatable variables. You need to think of this as a function: calculate the variable $n as a function of something in your input. It might be as simple as position(), or something more complicated involving xsl:number.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed transforming plist to XML by XSL Vartan XSLT 4 September 28th, 2007 03:11 PM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
differnce between xsl:apply-templates and xsl:call chandu.mca007 XSLT 2 June 12th, 2007 04:12 AM
<xsl:for-each> and key() - help needed muchofrio XSLT 5 April 13th, 2006 09:28 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM



All times are GMT -4. The time now is 07:56 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.