0

The following is part of the code in my input.xml file:

<info>
<name>
<surname>Sachin</surname>
<x> </x>
<given-names>J</given-names>
</name>
<x>, </x>
<name>
<surname>Sushant</surname>
<x> </x>
<given-names>K</given-names>
</name>
</info>

When I copy these nodes by using copy-of element as in

<xsl:copy-of select="info"></xsl:copy-of>

then the following output is generated:

<info>
<name>
<surname>Sachin</surname>
<x xml:space="preserve"> </x>
<given-names>J</given-names>
</name>
<x xml:space="preserve">, </x>
<name>
<surname>Sushant</surname>
<x xml:space="preserve"> </x>
<given-names>K</given-names>
</name>
</info>

I want to remove xml:space="preserve" from my output.xml file.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Sachin J
  • 2,081
  • 12
  • 36
  • 50
  • That's not a namespace. How do you generate your output? – Emiliano Poggi Jul 29 '11 at 13:15
  • Why do you want these removed? They explicitly declare that the space in the element needs to be there, which may be important. Many XML parsers would treat these as simply `` without it. – Flynn1179 Jul 29 '11 at 15:08

1 Answers1

3

xsl:copy-of makes an exact copy. If you want to make any changes at all, however small, then you need to use the "modified identity template" coding pattern. In this case, the following template rule should do it:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@* except xml:space"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

(The "except" operator is XPath 2.0 - if you're stuck with 1.0, use @*[name() != 'xml:space'])

Michael Kay
  • 156,231
  • 11
  • 92
  • 164