0

I want to convert XML to XML using XSLT in JAVA. How to add namespace name and it's value in XSLT file? I have tried many ways to get the namespace value but didn't get the output what i expect. So Please do the needful.

This is my XML,

<?xml version="1.0" encoding="ISO-8859-1"?>
<root xmlns="namespacename">
  <child>A</child>
  <child>B</child>
</root>

XSLT file,

<?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
       <xsl:element name="root" namespace="namespacename">
          <xsl:element name="child-one">
            <xsl:value-of select="root/child"/>
          </xsl:element>
       </xsl:element>
     </xsl:template>
    </xsl:stylesheet>

I need the output XML file like this.

<?xml version="1.0" encoding="ISO-8859-1"?>
<root xmlns="namespacename">
  <child-one>A</child-one>
</root>
Azhaguvel A
  • 631
  • 2
  • 10
  • 24
  • I need to add namespace name and it's value in the output XML but Simply they are displaying xml element without namespace in ---Transform XML to XML using xslt--- link. – Azhaguvel A Jul 25 '13 at 06:57
  • possible duplicate of [XSLT: Add namespace to root element](http://stackoverflow.com/questions/2686650/xslt-add-namespace-to-root-element) – Voicu Jul 25 '13 at 07:03

1 Answers1

2

If you know the namespace, then simply add it as the default namespace and write the result as literal elements.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="namespacename"
                xmlns:i="namespacename"
                exclude-result-prefixes="i">
  <xsl:template match="/">
    <root>
      <child-one>
        <xsl:value-of select="i:root/i:child"/>
      </child-one>
    </root>
  </xsl:template>
</xsl:stylesheet>

Note that the XPath expression root/child normally doesn't respect the default namespace, so you have to declare an additional namespace with a prefix (e.g. i) so the path becomes i:root/i:child. However, this also requires excluding the namespace for the result using exclude-result-prefixes="i".

obecker
  • 2,132
  • 1
  • 19
  • 23
  • Hi Obecker, Thanks for quick reply, I have checked this but it does not work. I need namespace in root element. Thanks. – Azhaguvel A Jul 25 '13 at 08:52
  • What do you mean with "it does not work"? The result I get is exactly the same as your example above (besides different encoding and missing line breaks) – obecker Jul 25 '13 at 09:16
  • Sorry Obecker, Problem in my browser version not in your code which (Browser) is not support namespace tag. Thank you so much..... – Azhaguvel A Jul 25 '13 at 11:24
  • Hi Obecker, One more help, Is this possible to add namespace in child element like A – Azhaguvel A Jul 25 '13 at 11:54
  • Having the same namespace declaration in both elements (in the parent and the child) doesn't make much sense. The namespace is already in scope on the child element. So in XSLT this is not possible and I doubt that any other (namespace aware) XML tool will do this. Of course, if the elements belong to different namespaces then there is no problem. – obecker Jul 25 '13 at 13:23
  • Different namespace only,For sample purpose, I have added like that. the actual namespace is in the child element... – Azhaguvel A Jul 25 '13 at 13:37