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 September 10th, 2008, 04:41 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default How to pass variables to collection(), document()

I'm using saxon9 and ant 1.7 on a PC.

I have a XSLT that refers to a directory that has multiple XML files in it. The stylesheet runs against the information in these XML files.

I've done this using the collection() function, as:

Code:
<xsl:variable name="subtreeCmdsource" select="collection('/Development/structured-fm/source/?select=*.xml')"></xsl:variable>

This was ok for testing, but now I need to make the script flexible and allow for the directory to be passed in as a stylesheet parameter from ant. The parameter from ant part is easy, but I can't figure out how to use the parameter in the collection() function.

Code:
<xsl:variable name="subtreeCmdsource" select="collection('$targetDir/?select=*.xml')"></xsl:variable>
Doesn't work.

I know you're probably slapping your head going "what an idiot", but this is what made sense to me.

Is there a way to do this?



------------------------
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old September 10th, 2008, 05:13 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The argument to collection() is a string, and you can construct the string at run-time using an XPath expression in the same way as any other string, for example

collection(concat($dir, '?select=*.xml'))

Clearly if $targetDir appears inside a string literal as in your example then it's not going to be treated as a variable reference.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old September 11th, 2008, 10:15 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Thanks Michael!

That was just what I needed.

------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old July 15th, 2009, 01:54 PM
Registered User
 
Join Date: Jul 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Question pass variables to document()

I'm somewhat new to XSLT, so please forgive my ignorance.

My issue is similar, but the concat suggestion isn't working for me. I have an XML file with links to other XML files that contain snippets of content. I'm trying to insert those content snippets into the parent XML file where the link references occur. I created a variable that captures the link reference (if one exists) using this code:

Code:
<xsl:variable name="snippetpath">
  <xsl:choose>
    <xsl:when test="contains(item[@name='ContentSection']/value,'Snippet')" >	
      <xsl:value-of select="item[@name='ContentSection']/value/p/snippet/@dcrpath"/>	
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="item[@name='ContentSection']/value"/>	
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>
I know it's capturing the correct information because I can see the links using this code:
Code:
<xsl:value-of select="$snippetpath"/>
When I pass the $snippetpath variable to the document function using this code:

Code:
<xsl:value-of disable-output-escaping="yes" select="document('$snippetpath')//Properties/Data/Datum[@ID='D02']/DCR[@Type='ContentSnippet']/record/item[@name='ContentArea']/value/item[@name='ContentSubSection']/value/item[@name='ContentSection']/value"/>
I get a message that the resource cannot be found. But, if I hard code the path into the document function, it works:

Code:
<xsl:value-of disable-output-escaping="yes" select="document('../SNIP/SPSx10879')//Properties/Data/Datum[@ID='D02']/DCR[@Type='ContentSnippet']/record/item[@name='ContentArea']/value/item[@name='ContentSubSection']/value/item[@name='ContentSection']/value"/>
I can't figure out what I'm doing wrong. Any suggestions are greatly appreciated!
 
Old July 15th, 2009, 01:59 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You want document($snippetlink) without the quotes around the variable reference.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
JessicaD (July 15th, 2009)
 
Old July 15th, 2009, 02:04 PM
Registered User
 
Join Date: Jul 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

THANK YOU!!! Of course it's the simplest of things... Thanks for your help!
 
Old July 15th, 2009, 02:25 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Also, disable-output-escaping="yes" is best avoided unless you really know what you are doing. When used by a beginner, it generally indicates a serious misunderstanding of how to achieve something quite simple.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old July 16th, 2009, 09:32 AM
Registered User
 
Join Date: Jul 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for the advice. I inherited the code and the project, so I'm not sure what was done before me or why. But in this case where I'm trying to pull in XML from another single-sourced file, would DOE be appropriate since I want the nodes from the single-sourced content? How else can I get it to recognize and parse the tags?

Thanks in advance for your help!
 
Old July 16th, 2009, 09:44 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You would need to disable-output-escaping if you have text with escaped markup e.g. with
Code:
<foo><![CDATA[<p>...<p>...<br>...]]></foo>
we have a 'foo' element in the XML that has as its content escaped HTML markup. If we want the stylesheet to create HTML output with the contents of the 'foo' element we can use
Code:
<xsl:template match="foo">
  <xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
as long as the XSLT processor supports that for the output you use it with.

But if you have real XML nodes in your input as in
Code:
<foo><p>...</p><p>...<br /></p></foo>
then you don't need disable-output-escaping, you can simply copy nodes to the output:
Code:
<xsl:template match="foo">
  <xsl:copy-of select="node()"/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
JessicaD (July 16th, 2009)
 
Old July 16th, 2009, 10:09 AM
Registered User
 
Join Date: Jul 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for the explanation. My input has real XML, so I removed the DOEs and changed the value-of to copy-of and it worked! Thanks so much!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass Variables obrienkev C# 2005 1 July 18th, 2008 03:58 AM
How do I Pass Session Variables cdanczak Classic ASP Professional 0 November 27th, 2007 10:42 AM
how can I pass variables to vb? astarti Beginning VB 6 4 September 5th, 2007 08:43 AM
Is it better to pass variables to a function? stephen_c_ Beginning PHP 3 November 4th, 2005 05:07 AM
How Do I Pass 2 Variables in Javascript Lucy Javascript 2 December 13th, 2003 11:25 AM





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