How to remove default namespace and keep the rest of namespaces using XSLT?
I have a XML file having default namespace and empty namespaces which need to be removed, while keeping the rest of namespaces.
Input:
<prefix:request xmlns:prefix="urn1" xmlns:foo2="urn2" xmlns="urn1">
<element1 xmlns="">version1</element1>
<element2 xsi:type="prefix:requestA" xmlns=""/>
...
</element1>
</prefix:request>
Expected output:
<prefix:request xmlns:prefix="urn1" xmlns:foo2="urn2">
<element1>version1开发者_开发百科</element1>
<element2 xsi:type="prefix:requestA"/>
...
</element1>
</prefix:request>
XSLT sample for removing namespaces will filter out all namespaces, including the prefix. Any idea how to solve this particular case?
This short transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="@*|namespace::*[name()]"/>
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
when applied on the (severely malformed and having to be corrected) provided XML-like input:
<prefix:request xmlns:xsi="Undefined !!!"
xmlns:prefix="urn1" xmlns:foo2="urn2"
xmlns="urn1">
<element1 xmlns="">version1</element1>
<element2 xsi:type="prefix:requestA" xmlns=""/> ...
</prefix:request>
produces the wanted, correct result:
<prefix:request xmlns:prefix="urn1" xmlns:xsi="Undefined !!!" xmlns:foo2="urn2">
<element1>version1</element1>
<element2 xsi:type="prefix:requestA"/> ...
</prefix:request>
Well the snippet that you have posted is not even well-formed XML so it is hard to tell what you want to achieve, as the second closing </element1>
does not have any corresponding opening tag and as the prefix xsi
is used but never declared.
However assuming you have
<prefix:request xmlns:prefix="urn1" xmlns:foo2="urn2" xmlns="urn1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<element1 xmlns="">version1</element1>
<element2 xsi:type="prefix:requestA" xmlns=""/>
</prefix:request>
then the stylesheet
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="namespace::*[local-name()]"/>
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@* | text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
when applied with Saxon 6.5.5 will output
<?xml version="1.0" encoding="utf-8"?><prefix:request xmlns:prefix="urn1" xmlns:foo2="urn2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<element1>version1</element1>
<element2 xsi:type="prefix:requestA"/>
</prefix:request>
精彩评论