XSLT 空白 xmlns=""变换后
我正在使用 XSLT 从一种 XML 标准转换为另一种标准。特定的结果 XML 标准包含一个根元素(属于命名空间的一部分)和一个子节点(属于另一个命名空间的一部分)。
转换成功反映了这些命名空间,但子级的子级现在包含空白 xmlns 属性。如何防止这种xmlns=""
?
XSLT 片段:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:apply-templates select="REQUEST_GROUP" />
</xsl:template>
<xsl:template match="REQUEST_GROUP">
<ONCORE_ERECORD xmlns="http://test.com">
<xsl:apply-templates select="REQUEST/PRIA_REQUEST/PACKAGE"/>
<PAYMENT PaymentType="ACH" />
<TRANSACTION_INFO _AgentKey="" _AgentPassword="" />
</ONCORE_ERECORD>
</xsl:template>
<xsl:template match="PACKAGE">
<DOCUMENT_RECORDATION xmlns="http://test2.org">
<xsl:apply-templates select="PRIA_DOCUMENT"/>
</DOCUMENT_RECORDATION>
</xsl:template>
<xsl:template match="PRIA_DOCUMENT">
<PRIA_DOCUMENT _PRIAVersion="1.2">
<xsl:attribute name="_Type">
<xsl:value-of select="@RecordableDocumentType"/>
</xsl:attribute>
<xsl:attribute name="_Code"/>
<xsl:apply-templates select="GRANTOR" />
<xsl:apply-templates select="GRANTEE" />
<xsl:choose>
<xsl:when test="count(PROPERTY) = 0">
<PROPERTY>
<xsl:attribute name="_StreetAddress">
<xsl:value-of select="@StreetAddress"/>
</xsl:attribute>
<xsl:attribute name="_StreetAddress2">
<xsl:value-of select="@StreetAddress2"/>
</xsl:attribute>
<xsl:attribute name="_City">
<xsl:value-of select="@City"/>
</xsl:attribute>
<xsl:attribute name="_State">
<xsl:value-of select="@State"/>
</xsl:attribute>
<xsl:attribute name="_PostalCode">
<xsl:value-of select="@PostalCode"/>
</xsl:attribute>
<xsl:attribute name="_County">
<xsl:value-of select="@County"/>
</xsl:attribute>
<xsl:apply-templates select="LEGAL_DESCRIPTION"/>
</PROPERTY>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="PROPERTY" />
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="count(PARTIES) = 0">
<PARTIES>
<_RETURN_TO_PARTY _UnparsedName="" _StreetAddress="" _StreetAddress2="" _City="" _State="" _PostalCode="" />
</PARTIES>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="PARTIES" />
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="EXECUTION" />
<xsl:apply-templates select="CONSIDERATION" />
<xsl:apply-templates select="RECORDABLE_DOCUMENT/_ASSOCIATED_DOCUMENT" />
<xsl:apply-templates select="EMBEDDED_FILE" />
</PRIA_DOCUMENT>
源 XML:
<REQUEST_GROUP PRIAVersionIdentifier="2.4">
<REQUEST>
<PRIA_REQUEST _Type="RecordDocuments">
<PACKAGE>
<PRIA_DOCUMENT PRIAVersionIdentifier="2.4" RecordableDocumentSequenceIdentifier="1" RecordableDocumentType="Mortgage">
结果 XML:
<?xml version="1.0" encoding="utf-8"?>
<ONCORE_ERECORD xmlns="http://test.com">
<DOCUMENT_RECORDATION xmlns="http://test2.org">
<PRIA_DOCUMENT _PRIAVersion="1.2" _Type="Mortgage" _Code="" xmlns="">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
发生这种情况是因为 PRIA_DOCUMENT 位于默认命名空间中,而其父 DOCUMENT_RECORDATION 位于非默认命名空间中。您必须将 PRIA_DOCUMENT 放在与其父级相同的命名空间中,否则序列化程序需要生成
xmlns=""
。请参阅 Michael Kay 的“XSLT 2.0 和 XPATH 2.0,第 4 版”,第 475 页,他在其中讨论了这种具体情况。
This is happening because PRIA_DOCUMENT is in the default namespace, while its parent DOCUMENT_RECORDATION is in a non-default namespace. You must put the PRIA_DOCUMENT in the same namespace as its parent, otherwise the serializer is required to generate
xmlns=""
.See Michael Kay's "XSLT 2.0 and XPATH 2.0, 4th edition", page 475 where he discusses this exact situation.
我找到了一个有效的解决方案,尽管它可能不是实现预期结果的最有效方法。
我只是将所有文字元素声明更改为:
并声明了名称空间。生成的xslt如下:
I found a solution that worked, though it may not have been the most efficient way to achieve the desired results.
I simply changed all literal element declarations to:
and declared the namespace. The resulting xslt is as follows:
我遇到了类似的问题,甚至在子元素上声明了命名空间,但最终
我仍然认为这是由于 xslt 转换造成的,但转换的字符串结果是正确的,然后我将字符串转换为 org .w3c.dom.正在添加默认名称空间的文档。
让 DocumentBuilderFactory 命名空间感知修复了这个问题
I was having a similar issue even declaring the namespace on the child elements but was still ending up with
I thought it was due to the xslt transformation but the string result of the transform was correct and it was when I then transformed the string to a org.w3c.dom.Document that the default namespaces were being added.
Making the DocumentBuilderFactory namespace aware fixed this
将调用模板和应用模板放在同一个命名空间中。
Put the calling template and the applied template in the same namespace.
您正在使用“xmlns=”声明重新定义每个节点的默认命名空间。由于
PRIA_DOCUMENT
没有命名空间,因此输出需要将其重新声明为空,否则它将具有与其父级相同的命名空间。我建议将命名空间添加到已定义的元素中,例如:和
有了这些命名空间,
PRIA_DOCUMENT
元素上的空白声明就变得不必要了,并且没有添加。You're re-defining the default namespace with each of those nodes with the 'xmlns=' declaration. Because the
PRIA_DOCUMENT
doesn't have a namespace, the output needs to redeclare it as empty, or it would have the same namespace as it's parent. I'd recommend adding in a named namespace to those elements that have one defined, for example:<pria:DOCUMENT_RECORDATION xmlns:pria="http://pria.org">
and
<as:ONCORE_ERECORD xmlns:as="http://aptitudesolutions.com">
With these named namespaces in place, the blank declaration on the
PRIA_DOCUMENT
element becomes unnecessary, and is not added.