5

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?

1
  • Good question, +1. See my answer for a very short (shortest so far) and easy solution :) Commented Aug 11, 2011 at 12:57

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

@sari: Do you mean that you get this error while applying my transformation on the XML document you provided in your question? If so, this means you are using a defective tool, uncompliant tool. The best thing to do is stop using it. There are several good XSLT developer tools and XSLT processors. In the Java world people use Saxon. Many developers use oXygen as their XSLT IDE.
Thank you. So far the transformation given does not work in Jdeveloper XSLT tool ;(. An error "XML-23049: FOTY0021: invalid node type" is generated. I have tried simpler script, like one for removing all namespaces from XML input. That works OK, no errors, but in the output root element there is a empty namespace, like this xmlns="". That is also unwanted result.
That's correct, I get "invalid node type" error also for the first answer on this post. Thank you for advices, it seems that I must now move to another tool. Will post here when get a fix.
Just providing the errmsg isn't sufficient. There must be a line number, on which this error was raised. Anyway, simply don't use this toy the same way you won't be using a broken bicycle.
1

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>

2 Comments

Yes, your assumption is right, thank you! Now, when tested your script in JDeveloper mediator XSLT 1.0 I get error. It says either the XSL defined does not match with payload, or payload is invalid; then it says invalid node type. But the payload in question is transformed all right by XSLT scripts for removing all namespaces. What could be wrong here?
Sorry, I don't know JDeveloper mediator so I can't help explaining its error messages.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.