XML namespace handling in xslt
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>
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>
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>
链接地址: http://www.djcxy.com/p/65710.html
上一篇: Google站点地图命名空间混乱
下一篇: xslt中的XML名称空间处理