英文:
How to suppress unnecessary xml namespace prefix declaration?
问题
I receive this xml
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</elem1>
</message>
and I need to transform it to this using xslt (prefix changed on nil attribute)
<?xml version="1.0" encoding="utf-8"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 xsi:nil="true"/>
</elem1>
</message>
I am aware that there is no semantic difference in these two. I have no control over the xml received and am informed that the output cannot have namespace declarations, other than on the root.
I have tried this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.ns1.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="i">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@i:nil">
<xsl:attribute name="xsi:nil">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
but I get this from it
<?xml version="1.0" encoding="utf-8"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http/www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 xsi:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</elem1>
</message>
The i prefix declaration on elem2 is unnecessary and not allowed by my client.
I can only modify the stylesheet.
Is this possible?
英文:
I receive this xml
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</elem1>
</message>
and I need to transform it to this using xslt (prefix changed on nil attribute)
<?xml version="1.0" encoding="utf-8"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 xsi:nil="true"/>
</elem1>
</message>
I am aware that there is no semantic difference in these two. I have no control over the xml received and am informed that the output cannot have namespace declarations, other than on the root.
I have tried this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.ns1.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="i">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@i:nil">
<xsl:attribute name="xsi:nil">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
but I get this from it
<?xml version="1.0" encoding="utf-8"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 xsi:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</elem1>
</message>
The i prefix declaration on elem2 is unnecessary and not allowed by my client.
I can only modify the stylesheet.
Is this possible?
答案1
得分: 2
您正在复制父元素,这意味着(在XSLT 1.0中)您也在复制其命名空间。
尝试使用以下方式:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="i">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@i:nil]">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
<xsl:apply-templates select="@*[not(name() = 'i:nil')] | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这是您提供的XML样式表的翻译部分。
英文:
You're copying the parent element and that means (in XSLT 1.0) you're copying its namespaces too.
Try perhaps:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="i">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@i:nil]">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
<xsl:apply-templates select="@*[not(name() ='i:nil')] | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
答案2
得分: 0
请尝试以下解决方案。
输入 XML
<?xml version="1.0"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</elem1>
</message>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.ns1.com" xmlns:ns1="http://www.ns1.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="i ns1">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:elem2">
<elem2 xsi:nil="{@i:nil}"/>
</xsl:template>
</xsl:stylesheet>
输出
<?xml version="1.0" encoding="utf-8"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 xsi:nil="true" />
</elem1>
</message>
英文:
Please try the following solution.
Input XML
<?xml version="1.0"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</elem1>
</message>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.ns1.com" xmlns:ns1="http://www.ns1.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="i ns1">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:elem2">
<elem2 xsi:nil="{@i:nil}"/>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="utf-8"?>
<message xmlns="http://www.ns1.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elem1 id="att1">
<elem2 xsi:nil="true" />
</elem1>
</message>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论