2

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>
1
  • Did you try searching for previous questions on this topic? This question is asked nearly every day, and the answers are easy to find. Commented Apr 18, 2013 at 10:43

2 Answers 2

2

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

Comments

1

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>

Comments

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.