当前位置: 动力学知识库 > 问答 > 编程问答 >

xml - Why does the <xsl:template match="/"> throw an error when adding a <xsl:result-document>?

问题描述:

With this source XML:

<?xml version='1.0' encoding='UTF-8'?>

<ns:Report_Data xmlns:ns="urn:someURN">

<ns:Report_Entry>

<ns:ReportName>SomeReportName</ns:ReportName>

<ns:ReportComments>Some description for this report....</ns:ReportComments>

<ns:AreaWhereUsed Descriptor="ThisIsATransform">

<ns:ID type="GUID">SomeUniqueID</ns:ID>

<ns:ID type="Component_ID">CUSTOM_COMPONENT</ns:ID>

</ns:AreaWhereUsed>

<ns:AreaWhereUsed Descriptor="SomeProcessName">

<ns:ID type="GUID">SomeDifferentUniqueID</ns:ID>

<ns:ID type="Process_ID">SomeProcessName</ns:ID>

</ns:AreaWhereUsed>

<ns:Area_Where_Used/>

<ns:Area_Where_Used>

<ns:AWU_Comment>This is some process description</ns:AWU_Comment>

</ns:Area_Where_Used>

<ns:CFs>

<ns:CF>SomeCF</ns:CF>

</ns:CFs>

</ns:Report_Entry>

</ns:Report_Data>

I am trying to convert it to a set of text documents. One document for each "Report Entry."

When I use this XSL:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns:ns="urn:someURN"

exclude-result-prefixes="xs" version="2.0">

<xsl:output method="text" omit-xml-declaration="yes"/>

<xsl:variable name="delimiter" select="'&#x09;'"/>

<xsl:variable name="quote" select="'&#x22;'"/>

<xsl:variable name="linefeed" select="'&#xA;'"/>

<xsl:template match="/">

<!-- Data Row Begins -->

<xsl:for-each select="/ns:Report_Data/ns:Report_Entry">

<xsl:text>Text and various other stuffs</xsl:text>

</xsl:for-each>

<!-- Data Row End -->

</xsl:template>

</xsl:stylesheet>

The output is that single line of text, which I mostly expect.

However, when I add a result-document tag, like so:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns:ns="urn:someURN"

exclude-result-prefixes="xs" version="2.0">

<xsl:output method="text" omit-xml-declaration="yes"/>

<xsl:variable name="delimiter" select="'&#x09;'"/>

<xsl:variable name="quote" select="'&#x22;'"/>

<xsl:variable name="linefeed" select="'&#xA;'"/>

<xsl:template match="/">

<!-- Data Row Begins -->

<xsl:for-each select="/ns:Report_Data/ns:Report_Entry">

<xsl:result-document method="text" href="file_{@ns:ReportName}-output.txt">

<xsl:text>Text and various other stuffs</xsl:text>

</xsl:result-document>

</xsl:for-each>

<!-- Data Row End -->

</xsl:template>

</xsl:stylesheet>

I get the error: Error: Internal error evaluating template rule at line 10 in module which refers to the template line: <xsl:template match="/"> I'm using the website http://xslttest.appspot.com/ for testing my transform.

I'm still pretty new to XSL, so I don't really understand what the template and apply-template tags do. I'd appreciate any pointers as to how the template tag is interacting with the result-document tag.

(Note, I've heavily sanitized the code, but it runs as stated above).

Thanks!

网友答案:

First of all, there is no namespace prefix used in the xpath in <xsl:for-each select="/Report_Data/Report_Entry">, but your input xml has it. So I doubt there is any output coming from your transformation.
You can specify a prefix for the namespace, urn:(someURN), and must use the same prefix with elements in the xpath.
e.g., xmlns:ns="urn:(someURN)"
<xsl:for-each select="/ns:Report_Data/ns:Report_Entry">

The use of a website(an external resource) could be the issue while using xsl:result-document as this instruction would instruct the processor to write a local file, which might not be possible.
I suggest you to use a local processor and try the transformation.

网友答案:

The message

Error: Internal error evaluating template rule

often indicates an internal bug within Saxon that should be reported to Saxonica. However, it's possible that it could be triggered from a "callback" method such as the OutputURIResolver. It's conceivable, for example, that the people implementing this online service would use an OutputURIResolver that throws an exception unconditionally when xsl:result-document is called (though there are cleaner ways of configuring Saxon to achieve the same effect).

So I would suggest, report it to Saxonica only if you can reproduce it in your own environment where you know exactly how Saxon has been configured.

分享给朋友:
您可能感兴趣的文章:
随机阅读: