4

I'm trying to make a transformation for an XML document but i cannot find a solution since i do not know XSLT. I have the XML document:

<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns="http://www.test.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation='http://whatever/test.xsd'>

  <address>
    <name>Joe Tester</name>
    <street>Baker street 5</street>
  </address>

</addresses>

and I want to produce:

<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns="http://www.test.org/xml">

  <address>
    <name>Joe Tester</name>
    <street>Baker street 5</street>
  </address>

</addresses>

(Consider that xsi:noNamespaceSchemaLocation="..." has already excluded using another XSLT before this one).

Can someone help me find a solution?

The XSLT used to eliminate the xsi:noNamespaceSchemaLocation is:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
 <xsl:copy>
  <xsl:apply-templates select="@* | node()" />
 </xsl:copy>
</xsl:template>

<xsl:template match="@xsi:noNamespaceSchemaLocation"/>

</xsl:stylesheet>
2
  • How about cribbing off of the 'other XSLT' indicated in your post to eliminate the bad attribute?
    – hd1
    Jan 7, 2013 at 0:02
  • @hd1 - The remaining part user466825 wants to omit is a namespace declaration, not an attribute.
    – JLRishe
    Jan 7, 2013 at 1:05

3 Answers 3

4

I have got two options:

XSLT1:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="@*[local-name(.)='noNamespaceSchemaLocation']"/>
</xsl:stylesheet>

Output1:

<?xml version="1.0" encoding="utf-8"?>
<addresses xmlns="http://www.test.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <address>
    <name>Joe Tester</name>
    <street>Baker street 5</street>
  </address>
</addresses>

XSLT2:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@*[local-name(.)='noNamespaceSchemaLocation']"/>
</xsl:stylesheet>

Output2:

<?xml version="1.0" encoding="utf-8"?>
<addresses>
  <address>
    <name>Joe Tester</name>
    <street>Baker street 5</street>
  </address>
</addresses>
3
  • My need is to remove the xmlns:xsi=".." from the root element without removing the xmlns, or any other xmlns:* . Jan 7, 2013 at 18:35
  • Namespace declaration, unlike any other attribute won't be treated normally! You may not want the declaration to be there in output XML but, XSLT parser thinks it is needed (because it is possible that you might have used the namespace as prefix to some element in your XML!) Jan 8, 2013 at 4:54
  • so you either have to get rid of xmlns from your whole XML or get rid of just one element/attribute (or multiple) which are using that namespace prefix! Jan 8, 2013 at 4:55
1

Please give this a try:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsi"
>

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <xsl:copy-of select="namespace::*[not(. = 'http://www.w3.org/2001/XMLSchema-instance')]" />
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@xsi:noNamespaceSchemaLocation"/>

</xsl:stylesheet>
2
  • Unfortunately, the xmlns:xsi=".." is not removed. Jan 7, 2013 at 18:32
  • You're right. Ian Roberts' solution was one step closer than mine, but it looks like he still had one oversight. Please try the modified approach above.
    – JLRishe
    Jan 8, 2013 at 2:43
0

Your problem is that when copying an element node <xsl:copy> implicitly copies namespace nodes that are in scope at that point in the document. Try adding an extra template for element nodes to specifically exclude the xsi namespace:

<xsl:template match="*">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:copy-of select="namespace::*[not(name() = 'xsi')]" />
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

If your xsl:stylesheet has an xmlns:xsi then you may also find you need to add exclude-result-prefixes="xsi" as suggested in this answer.

This should prevent the xsi namespace from appearing in the output if it is there solely because it was copied from the input, though the serialiser may re-introduce it if required to make the output well formed (i.e. if it needs to output an element or attribute in that namespace).

8
  • This almost worked, but it seems that it doesn't work because self::xsi is a node test for an element named xsi. not(name() = 'xsi') or not(. = 'http://www.w3.org/2001/XMLSchema-instance') should do the job though.
    – JLRishe
    Jan 8, 2013 at 2:47
  • I think you need a template to drop @xsi:noNamespaceSchemaLocation .. otherwise the value shows up in output as orphan, because you are using <xsl:apply-templates select="@*|node()"/> Jan 8, 2013 at 4:56
  • 1
    @InfantProgrammer'Aravind' OP said they'd already removed that in another stylesheet. Jan 8, 2013 at 11:32
  • Agreed but May be he has misinterpreted ! observing the input and desired output XML samples, it looks like OP wants to get rid of namespace declaratoin using XSLT! Also the same thing he has commented on other answers as well. Jan 8, 2013 at 13:08
  • 1
    @InfantPro'Aravind' yes, and my point is that removing all uses of the namespace is not sufficient to achieve this, because <xsl:copy> copies namespace nodes (i.e. in-scope declarations from the source XML) along with the element it is copying. XSLT 2.0 allows <xsl:copy copy-namespaces="no"> but that isn't an option in XSLT 1.0. Jan 8, 2013 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.