Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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
  #1 (permalink)  
Old February 27th, 2006, 07: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>



  #2 (permalink)  
Old February 27th, 2006, 07:17 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
  #3 (permalink)  
Old February 27th, 2006, 07: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

  #4 (permalink)  
Old February 27th, 2006, 08:02 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
  #5 (permalink)  
Old February 27th, 2006, 08: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>

  #6 (permalink)  
Old February 27th, 2006, 09: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>


  #7 (permalink)  
Old February 28th, 2006, 05:46 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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


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





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