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 July 5th, 2008, 05:17 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default document() and param - help please

Hi,

Below is my XSLT against SQL_XML which contains some xpath instruction in the text node. The job of the XSLT is to interpret the xpath instruction and locate the data on another XML called TP_XML and format the result combining the attribute information from both xml files.

I used document() and param to call the TP_XML and pass the xpath instruction to retrieve the data.

Unfortunately, my IDE is telling me I have some errors and it won't tell me how to spot and fix them.

Please help.


Source:
=======
(1) TP_XML.xml
<values>
<field name="TP_field1">TP field1 value</field>
<field name="TP_field2">TP field2 value</field>
<field name="TP_field3">TP field3 value</field>
<field name="TP_field4">TP field4 value</field>
</values>

(2) SQL_XML processed by a local XSLT:

<values>
<field name="SQL_field1">//field[@name='TP_field1']</field>
<field name="SQL_field2">//field[@name='TP_field2']</field>
<field name="SQL_field3">//field[@name='TP_field3']</field>
<field name="SQL_field4">//field[@name='TP_field4']</field>
</values>

Output :
========
<field name>with attribute data from SQL_XML above
and text() data from TPXML.xml

<values>
<field name="SQL_field1">TP field1 value</field>
<field name="SQL_field2">TP field2 value</field>
<field name="SQL_field3">TP field3 value</field>
<field name="SQL_field4">TP field4 value</field>
</values>

-->

<xsl:template match="values">
    <xsl:copy>



    <xsl:for-each select="//field">
         <xsl:copy>
        <xsl:attribute name="field">
            <xsl:value-of select="@name"/>
        </xsl:attribute>
        <xsl:call-template name="field">
            <xsl:with-param name="xpath_node_TPXML"/>
        </xsl:call-template>
         </xsl:copy>
    </xsl:for-each>
    </xsl:copy>
</xsl:template>



<xsl:template match="field">
<xsl:param name="xpath_node_TPXML" select="text()"/>



   <xsl:value-of select="document('TP_XML.xml')$xpath_node_TPXML"/>

</xsl:template>
</xsl:stylesheet>
 
Old July 5th, 2008, 06:09 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I haven't analysed your code fully but, at first glance, there are two errors:
* You call a template named "field" but there isn't one with that name. There is one that matches "field". You either need to apply-templates and select "field" or remove the match attribute on the template and name it "field".

* You try to use a variable holding a string value as an XPath expression. $xpath_node_TPXML is a string and just because what it holds is a string representation of XPath doesn't mean you can interchange them. If you need to do that then you can either use an extension function, Saxon for instance has the saxon:evaluate function or it's easy enough to write your own in JScript or C# if using a Microsoft processor. Alternatively you can perform two transformations, one to create a new XSLT that has the XPaths retrieved from SQL_XML embedded in the transform. For example create a number of variables such as: xsl:variable name="field1" select="//field[@name='TP_field1']". Then run this transform against the source document.

--

Joe (Microsoft MVP - XML)
 
Old July 5th, 2008, 06:35 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe for the immediate response, much appreciated specially when I'm at work on a weekend.

You are right in spotting the error in "template match". I should have used <xsl:template name="field">

I will look into your recommendation to use <xsl:variable> and "creating another XSLT that has the XPaths retrieved from SQL_XML".

Regards!








Similar Threads
Thread Thread Starter Forum Replies Last Post
question on with-param anboss XSLT 6 June 23rd, 2008 05:58 PM
Reassign param value? victorcorey XSLT 2 November 29th, 2007 05:33 AM
with-param nodeset RoeZ XSLT 6 November 1st, 2007 06:53 PM
Param Question Tre XSLT 7 March 27th, 2007 09:13 AM





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