tags:

views:

16

answers:

3

I currently have a string (XSLT 1.0) which I'd like to turn into a NodeList so that it can be used the following example.

($testString represents an element in a XML tree)

<xsl:apply-templates select="$testString">

<xsl:template match="$testString">

I have tried using the following but neither seem to work

<xsl:apply-templates select="xx:node-set($testString)">
<xsl:apply-templates select="exslt:node-set($testString)">

Is there a away to convert the string to another variable which ahs NodeList properties? Or is there anyway around this?

Update: I currently have a JSP which calls an XSL using XML. The JSP page takes in a string which is passed to the XSL page. The string represents an element in the XML. I am trying to search through each Node looking for that element and return the value of the element.

A: 

I currently have a string (XSLT 1.0) which I'd like to turn into a NodeList

A string is not a node. The only way to make a node from a string is to try to parse it with an XML parser. This cannot be done dynamically within an XSLT 1.0 or 2.0 transformation.

If the string is statically defined and it represents a well-formed XML fragment, then it can be specified as the body of an <xsl:variable> or an <xsl:param> instruction and converted to a regular node-set by using the xxx:node-set() extension function provided by the particular vendor, as shown in more detail in @Alejandro's answer.

If you provide more details about the task you want to solve (XML document, wanted result, how the result is related to the source XML document), then many people would be able to help.

Dimitre Novatchev
The string is a parameter being sent from JSP, can I declare it as a node in JSP? The idea is to allow the user to search for a particular element with in the xml file
Hammer
@Hammer: Please, update /re-write the question and provide all the detail. Almost anything can be done with XSLT if the complete problem is defined.
Dimitre Novatchev
A: 

For XSLT 1.0 and XSLT 2.0, Pattern in rules should match nodes. In XSLT 1.0 shouldn't have variable/param reference.

node-set extension function converts a Result Tree Fragment into a node: the node root of the RTF.

So, in order to do what you want, you should use:

<xsl:variable name="vStringRTF">
   <!-- Your string --> 
</xsl:variable> 

<xsl:apply-templates select="exslt:node-set($vStringRTF)/node()"/> 

<xsl:template match="text()"/> 
Alejandro
A: 

Have you got the exslt namespace declaration in your stylesheet? The following works for me with any xml input (libxslt / xsltproc)

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exslt="http://exslt.org/common"
    version="1.0">

    <xsl:template match="/">
        <xsl:variable name="xml-string"><foo bar="baz">etc...</foo></xsl:variable>
        <xsl:variable name="xml-node" select="exslt:node-set($xml-string)"/>
        Bar is <xsl:value-of select="$xml-node//@bar"/>
    </xsl:template>

</xsl:stylesheet>
Robin