XML namespace handling in xslt

karthik Source

i need to convert from one xml format to another xml using xslt.Initially i tried without namespace and i got it. but when i tried with name space xmlns="http://ws.wso2.org/dataservice" it not working without prefixes as xmlns:d="http://ws.wso2.org/dataservice" in the given xml file

<test xmlns="http://ws.wso2.org/dataservice">
 <datarows>
  <name>Name</name>
 </datarows>
 <datarows>
  <name>karthik</name>
 </datarows>
 </testcsv>

i need xml file after xsl transformation as like

<head>
<names>Name</names>
<names>karthik</names>
</head>

Help me with xslt

i tried xslt as

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:d="http://ws.wso2.org/dataservice" exclude-result-prefixes="d" >
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
     <namespace-alias stylesheet-prefix="xsl" result-prefix="#default"/>


    <xsl:template match="/">
    <head>
        <xsl:for-each select="testcsv/datarows">
            <names>
                <xsl:value-of select="name" />
            </names>
        </xsl:for-each>
        </head>
    </xsl:template>
</xsl:stylesheet>
xmlxsltnamespaces

Answers

answered 5 years ago siva2012 #1

Please change the below list of things: 1. Your Input XML is not well formed. Please Change the root element to <testcsv> 2. Remove <namespace-alias stylesheet-prefix="xsl" result-prefix="#default"/> from your XSLT as it is not needed. 3. Add namespace xmlns:d="http://ws.wso2.org/dataservice" in your Input XML

Input XML:

<?xml version="1.0"?>
<testcsv xmlns:d="http://ws.wso2.org/dataservice">
 <datarows>
  <name>Name</name>
 </datarows>
 <datarows>
  <name>karthik</name>
 </datarows>
 </testcsv>

XSLT transformation:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:d="http://ws.wso2.org/dataservice" exclude-result-prefixes="d" >
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
    <head>
        <xsl:for-each select="testcsv/datarows">
            <names>
                <xsl:value-of select="name" />
            </names>
        </xsl:for-each>
        </head>
    </xsl:template>
</xsl:stylesheet>

Output:

<head>
  <names>Name</names>
  <names>karthik</names>
</head>

answered 5 years ago hr_117 #2

As I understand you would (or could) not change the xmlns declaration in your xml (perhaps because it is generated somewhere else). Than you have to use the xml node prefix in your xlst.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:d="http://ws.wso2.org/dataservice"
    exclude-result-prefixes="d"
    >
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <head>
            <xsl:for-each select="d:testcsv/d:datarows" >
                <names>
                    <xsl:value-of select="d:name" />
                </names>
            </xsl:for-each>
        </head>
    </xsl:template>
</xsl:stylesheet>

answered 2 months ago tekblade #3

How it should be when input xml file :

xmlns:d="http://ws.wso2.org/dataservice"

in my code is something:

xmlns="http://ws.wso2.org/dataservice"

without prefix. regards

comments powered by Disqus