[英]remove bad attributes from xml via xslt
i have some xml and i am trying to remove bad attributes that some other xslt add them. 我有一些xml,并且我正在尝试删除一些其他xslt添加它们的不良属性。
<?xml version="1.0" encoding="UTF-8"?><MYXML xmlns="http://someURL/MYXML" xmlns:role="http://someURL/role"> <firstElement> <LANGUAGE>Spanish</LANGUAGE> <VOCABULARY>Spain</VOCABULARY> </firstElement> <External xmlns="" xmlns:myxml="http://someURL/MYXML"> <EXTARData/> <AnotherElementData> <AnotherElement xmlns="http://someURL/AnotherElement"/> </AnotherElementData> </External> </MYXML><!--checksum=A477829F524D170104E87187AD2869F5443DD079196B0EDCD5C6B9CFDD315232 version=1-->
i would like to rid off the "xmlns="" xmlns:myxml="http://someURL/MYXML"" without success. 我想摆脱“ xmlns =”“ xmlns:myxml =” http:// someURL / MYXML“”但没有成功。
my expected results is: 我的预期结果是:
<?xml version="1.0" encoding="UTF-8"?><MYXML xmlns="http://someURL/MYXML" xmlns:role="http://someURL/role"> <firstElement> <LANGUAGE>Spanish</LANGUAGE> <VOCABULARY>Spain</VOCABULARY> </firstElement> <External > <EXTARData/> <AnotherElementData> <AnotherElement xmlns="http://someURL/AnotherElement"/> </AnotherElementData> </External> </MYXML><!--checksum=A477829F524D170104E87187AD2869F5443DD079196B0EDCD5C6B9CFDD315232 version=1-->
could someone help me with it? 有人可以帮我吗?
So you seem to want to transform the elements in no namespace to the namespace of the root element: 因此,您似乎想将没有名称空间的元素转换为根元素的名称空间:
<xsl:template match="*[namespace-uri() = '']">
<xsl:element name="{local-name()}" namespace="{namespace-uri(/*)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
That together with the identity transformation gives 与身份转换一起
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[namespace-uri() = '']">
<xsl:element name="{local-name()}" namespace="{namespace-uri(/*)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
and results in https://xsltfiddle.liberty-development.net/eiZQaFX 并产生https://xsltfiddle.liberty-development.net/eiZQaFX
<MYXML xmlns="http://someURL/MYXML" xmlns:role="http://someURL/role">
<firstElement>
<LANGUAGE>Spanish</LANGUAGE>
<VOCABULARY>Spain</VOCABULARY>
</firstElement>
<External>
<EXTARData />
<AnotherElementData>
<AnotherElement xmlns="http://someURL/AnotherElement" xmlns:myxml="http://someURL/MYXML" />
</AnotherElementData>
</External>
</MYXML><!--checksum=A477829F524D170104E87187AD2869F5443DD079196B0EDCD5C6B9CFDD315232 version=1-->
As you see, that is not quite the result you have shown as the identity transformation in XSLT 1 copies the namespace xmlns:myxml="http://someURL/MYXML"
that was in scope for the AnotherElement
. 正如你看到的,这是不太你已经显示为XSLT身份转换1份命名空间中的结果
xmlns:myxml="http://someURL/MYXML"
那是在范围为AnotherElement
。
That problem can be avoided easily with an XSLT 2 or 3 processor by using 通过使用XSLT 2或3处理器,可以轻松避免该问题。
see https://xsltfiddle.liberty-development.net/eiZQaFX/1 . 参见https://xsltfiddle.liberty-development.net/eiZQaFX/1 。
With an XSLT 1 processor you would need more effort to make sure the elements are copied without any namespaces: 使用XSLT 1处理器,您将需要更多的工作来确保复制元素时没有任何名称空间:
<xsl:template match="*[namespace-uri() = '']//*[namespace-uri() != '']">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
Complete example is 完整的例子是
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[namespace-uri() = '']">
<xsl:element name="{local-name()}" namespace="{namespace-uri(/*)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*[namespace-uri() = '']//*[namespace-uri() != '']">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
at https://xsltfiddle.liberty-development.net/eiZQaFX/2 在https://xsltfiddle.liberty-development.net/eiZQaFX/2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.