简体   繁体   English

Java XML 和 XSLT 转换错误:尚未声明前缀“m”的命名空间

[英]Java XML and XSLT transformation error: Namespace for prefix 'm' has not been declared

I'm trying to transform an XML document with the javax.xml.transform.Transformer and XSLT, and am having trouble with a namespace prefix that is not recognized when I call the transform method.我正在尝试使用 javax.xml.transform.Transformer 和 XSLT 转换 XML 文档,并且在调用转换方法时无法识别命名空间前缀时遇到问题。

Here is the XML document which has the "m" namespace prefix defined in it:这是其中定义了“m”命名空间前缀的 XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
  <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
    <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="SITUATION">
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

Here is the XSL that currently just copies the whole XML document (later I'd like to extend it to merge in another XML document, similar to what is described here: https://stackoverflow.com/a/5706319/208011 ):这是目前仅复制整个 XML 文档的 XSL(稍后我想扩展它以合并另一个 XML 文档,类似于此处描述的内容: https : //stackoverflow.com/a/5706319/208011 ):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Copy everything including attributes as default action -->
    <xsl:template match="*">
        <xsl:element name="{name()}">
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
    </xsl:template>

</xsl:stylesheet>

Here is the java code:这是Java代码:

Source xsltSource = new StreamSource(new File(getClass().getClassLoader().getResource("merge-metadata.xsl").getFile()));
transformer = transFact.newTransformer(xsltSource);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document input = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(s1)));
StringWriter out = new StringWriter();
transformer.transform(new DOMSource(input), new StreamResult(out));

Here's the stack trace:这是堆栈跟踪:

Caused by: java.lang.RuntimeException: Namespace for prefix 'm' has not been declared.
    at com.sun.org.apache.xml.internal.serializer.SerializerBase.getNamespaceURI(SerializerBase.java:915)
    at com.sun.org.apache.xml.internal.serializer.SerializerBase.addAttribute(SerializerBase.java:431)
    at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.addAttribute(ToUnknownStream.java:316)

Eventually I want to merge this XML document with another, but I can't get past the namespace issue to just copy this one.最终我想将这个 XML 文档与另一个合并,但我无法解决命名空间问题而只复制这个文档。

If I don't use the xsl stylesheet in the transformer factory newTransformer() method then I don't get the namespace error and the output of the transformation is exactly the same as the original XML document.如果我不在转换器工厂 newTransformer() 方法中使用 xsl 样式表,那么我不会收到命名空间错误,并且转换的输出与原始 XML 文档完全相同。

If I set the DocumentBuilderFactory to not be namespace aware then I don't get the exception, but the output of the transformation is missing the namespaces.如果我将 DocumentBuilderFactory 设置为不感知命名空间,那么我不会收到异常,但转换的输出缺少命名空间。

Thanks for your help.谢谢你的帮助。

Your hand-crafted, pseudo-identity transform is losing the namespace declarations and failing when it tries to name an element with an undeclared namespace prefix:您手工制作的伪标识转换正在丢失命名空间声明,并且在尝试使用未声明的命名空间前缀命名元素时失败:

<xsl:element name="{name()}">

Use the standard identity transformation instead:改用标准身份转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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