如何抑制不必要的XML命名空间前缀声明?

huangapple go评论127阅读模式
英文:

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:

&lt;xsl:stylesheet version=&quot;1.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
exclude-result-prefixes=&quot;i&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; indent=&quot;yes&quot;/&gt;

&lt;xsl:template match=&quot;@* | node()&quot;&gt;
    &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select=&quot;@* | node()&quot;/&gt;
    &lt;/xsl:copy&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;*[@i:nil]&quot;&gt;
	&lt;xsl:element name=&quot;{name()}&quot; namespace=&quot;{namespace-uri()}&quot;&gt;
		&lt;xsl:attribute name=&quot;xsi:nil&quot;&gt;true&lt;/xsl:attribute&gt;
		&lt;xsl:apply-templates select=&quot;@*[not(name() =&#39;i:nil&#39;)] | node()&quot;/&gt;
	&lt;/xsl:element&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

答案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

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;message xmlns=&quot;http://www.ns1.com&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  &lt;elem1 id=&quot;att1&quot;&gt;
    &lt;elem2 i:nil=&quot;true&quot; xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; /&gt;
  &lt;/elem1&gt;
&lt;/message&gt;

XSLT

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
                xmlns=&quot;http://www.ns1.com&quot; xmlns:ns1=&quot;http://www.ns1.com&quot;
                xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
                xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
                exclude-result-prefixes=&quot;i ns1&quot;&gt;
	&lt;xsl:output method=&quot;xml&quot; indent=&quot;yes&quot;/&gt;

	&lt;xsl:template match=&quot;@* | node()&quot;&gt;
		&lt;xsl:copy&gt;
			&lt;xsl:apply-templates select=&quot;@* | node()&quot;/&gt;
		&lt;/xsl:copy&gt;
	&lt;/xsl:template&gt;

	&lt;xsl:template match=&quot;ns1:elem2&quot;&gt;
		&lt;elem2 xsi:nil=&quot;{@i:nil}&quot;/&gt;
	&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

Output

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;message xmlns=&quot;http://www.ns1.com&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  &lt;elem1 id=&quot;att1&quot;&gt;
    &lt;elem2 xsi:nil=&quot;true&quot; /&gt;
  &lt;/elem1&gt;
&lt;/message&gt;

huangapple
  • 本文由 发表于 2023年3月3日 23:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定