Hairi
Hairi

Reputation: 3733

XSLT takes escape sequences as parameter

Hi I have an stylsheet that that takes a parameter from an ant xslt task. But the parameter is full of escape sequences such as <tag-name> etc... Here is the raw representation of the parameter $fileset-xml: link which is a direct output from this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="fileset-xml"/>
    <!--  -->
    <!--  -->
    <xsl:template match="/theRoot">
        <root>
            <xsl:value-of select="$fileset-xml"/>
        </root>
    </xsl:template>
    <!-- -->
    <!-- -->
</xsl:stylesheet>

My intention is to produce an output in xml format, out of that parameter, but the function saxon:parse($fileset-xml) doesn't like the argument in this format. I guess that if we replace the escape characters with their rendered values I will get a valid xml file, but how to do that?

The processor is saxonb9-1-0-8j

EDITION_1

As an apendx to the comment of @MartinHonnen :

Processor:

<classpath location="../../infrastructure/SaxonEE9-7-0-11J/saxon9ee.jar:/net.sf.saxon.TransformerFactoryImpl"/>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="fileset-xml"/>
    <xsl:variable name="file-set-xml" select="parse-xml(parse-xml-fragment('&lt;a/&gt;'))"/>
    <!--  -->
    <!--  -->
    <xsl:template match="/theRoot">
        <root>
            <xsl:copy-of select="$file-set-xml"/>
        </root>
    </xsl:template>
    <!-- -->
    <!-- -->
</xsl:stylesheet>

And the error:

[xslt] : Fatal Error! Error checking type of the expression 'funcall(parse- xml-fragment, [literal-expr(<a/>)])'. Cause: Error checking type of the expressi on 'funcall(parse-xml-fragment, [literal-expr(<a/>)])'.

Upvotes: 0

Views: 592

Answers (1)

Michael Kay
Michael Kay

Reputation: 163625

If you supply a string in the form &lt;a/&gt; (10 characters), then the XPath function parse-xml-fragment() will turn this into the string <a/> (4 characters), and if you apply parse-xml() (or parse-xml-fragment()) to this 4-character string, you will get a node tree containing an empty element named "a".

So you need to parse the data twice.

Upvotes: 1

Related Questions