简体   繁体   中英

“[xX][mM][lL]” is not allowed. - creating XSLT file within String

I know there was a lot of these in this forum but none of the answers solved my problem.

I have a class that dynamically generates xslt file (don't ask why, I have lot of webservice responses to process and I found XSLT as the fastest way to do it and I do not want to create xslt file for every single webservice response).

So I am creating my XSLT in class XSLT writer using string builder, I get correct output

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:com.workday/bsvc"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
    <xsl:output method="text" indent="yes"/>
    <xsl:variable name="singleQ">
        <xsl:text>'</xsl:text>
    </xsl:variable>
    <xsl:variable name="doubleQ">
        <xsl:text>''</xsl:text>
    </xsl:variable>
    <xsl:template match="/">
        <file>
            <xsl:for-each select="wd:Get_Frequencies_Response/wd:Response_Data/wd:Frequency">
                <xsl:text>{'</xsl:text>
                <xsl:value-of
                    select="replace(wd:Frequency_Data/wd:Annualization_Factor, $singleQ, $doubleQ)"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of
                    select="replace(wd:Frequency_Data/wd:Frequency_Behavior_Reference, $singleQ, $doubleQ)"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of
                    select="replace(wd:Frequency_Data/wd:Frequency_ID, $singleQ, $doubleQ)"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of select="replace(wd:Frequency_Data/wd:Name, $singleQ, $doubleQ)"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of
                    select="replace(wd:Frequency_Data/wd:Use_Weekly_Hours, $singleQ, $doubleQ)"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of
                    select="replace(wd:Frequency_Data/wd:Used_in_Payroll_Interface, $singleQ, $doubleQ)"/>
                <xsl:text>'},</xsl:text>
            </xsl:for-each>
        </file>
    </xsl:template>
</xsl:stylesheet>

I know that this one is correct because I paste it into Oxygen xml and format it and ^ this is what I get. (Originally XSLT is written in ONE line, so I format it in Oxygen just to check if the output is OK).

Then I use this method to create my transformer:

    private Transformer createTransformer(XSLTWriter xslt) throws Exception {
    Transformer transformer = null;
    TransformerFactory tf = TransformerFactory.newInstance();
    Source s = new StreamSource(new StringReader(xslt.getXSLT()));
    transformer = tf.newTransformer(s);
    return transformer;
}

And I get an error for the XSLT I have pasted above :

    11:16:24,432 ERROR [stderr] (Get Frequencies) Error on line 1 column 1276
11:16:24,433 ERROR [stderr] (Get Frequencies)   SXXP0003: Error reported by XML parser: The processing instruction target matching
11:16:24,434 ERROR [stderr] (Get Frequencies)   "[xX][mM][lL]" is not allowed.
11:16:24,435 ERROR [stderr] (Get Frequencies) javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected.
11:16:24,436 ERROR [stderr] (Get Frequencies)   at net.sf.saxon.PreparedStylesheet.prepare(PreparedStylesheet.java:140)
11:16:24,437 ERROR [stderr] (Get Frequencies)   at net.sf.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:135)
11:16:24,438 ERROR [stderr] (Get Frequencies)   at net.sf.saxon.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:86)
11:16:24,439 ERROR [stderr] (Get Frequencies)   at __redirected.__TransformerFactory.newTransformer(__TransformerFactory.java:132)
11:16:24,440 ERROR [stderr] (Get Frequencies)   at config.loader.ConfigLoader.createTransformer(ConfigLoader.java:235)
11:16:24,440 ERROR [stderr] (Get Frequencies)   at config.loader.ConfigLoader.run(ConfigLoader.java:102)
11:16:24,442 ERROR [stderr] (Get Frequencies)   at java.lang.Thread.run(Thread.java:748)

Has anyone ever done that?

The error message makes it clear there is a processing instruction (or XML declaration) of the form <?xml ....?> at position 1276 in the source stylesheet you are supplying. This isn't allowed (if it's an XML declaration it must be at the start, if it's a processing instruction then it mustn't be called "xml").

Now the input you've shown doesn't contain this, so we have to conclude that the input you have shows us isn't the input that you are actually processing.

The fact that the input you've shown doesn't include an end tag for the xsl:stylesheet element might be a clue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM