Inline xslt namespace from parent nodes
This is actually my inline xslt:
<xsl:element name="ns0:LineItems" >
<xsl:element name="ns0:Response" >
<xsl:for-each select="//*[local-name()='Response']//*[local-name()='ITEMS']" >
<xsl:element name="ns0:ITEMS" >
<xsl:attribute name ="LINEITEMID" >
<xsl:value-of select="*[local-name()='LINEITEMID']" />
</xsl:attribute>
<xsl:attribute name ="ITM_NUMBER" >
<xsl:value-of select="*[local-name()='ITM_NUMBER']" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
My question is, how can i create the new nodes with the same namespace from the result xml ns0:elmentname
. When this namespace change to ns1:elmentname
, I have a problem.
The result should look like this:
<?xml version="1.0" encoding="utf-8"?>
<ns0:LineItems xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003">
<ns0:Response>
<ns0:ITEMS LINEITEMID="777" ITM_NUMBER="001000" />
<ns0:ITEMS LINEI开发者_开发知识库TEMID="2" ITM_NUMBER="001055" />
<ns0:ITEMS LINEITEMID="3" ITM_NUMBER="001020" />
</ns0:Response>
</ns0:LineItems>
My Input:
<?xml version="1.0" encoding="utf-8"?>
<Response>
<HEADER>
<ORDER_ID>1234</ORDER_ID>
</HEADER>
<E_SALESDOCUMENT>test123</E_SALESDOCUMENT>
<E_STATUS>AllesToll</E_STATUS>
<ITEMS>
<LINEITEMID>777</LINEITEMID>
<ITM_NUMBER>001000</ITM_NUMBER>
<MATERIAL>000000000000219701</MATERIAL>
</ET_ORDER_ITEMS>
<ET_ORDER_ITEMS>
<LINEITEMID>2</LINEITEMID>
<ITM_NUMBER>001055</ITM_NUMBER>
<MATERIAL>000000000000219702</MATERIAL>
</ET_ORDER_ITEMS>
<ET_ORDER_ITEMS>
<LINEITEMID>3</LINEITEMID>
<ITM_NUMBER>001020</ITM_NUMBER>
<MATERIAL>000000000000219703</MATERIAL>
</ITEMS>
</Response>
You can use namespace
of <xsl:element>
and determine current namespace using XPath namespace-uri(node-set)
function.
Example:
Suppose we have XML like this:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:a="a" xmlns:b="b">
<a:item>
<a:name>a</a:name>
</a:item>
<b:item>
<b:name>b</b:name>
</b:item>
</root>
So, we could use this XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="a" xmlns:b="b"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="root">
<root>
<xsl:apply-templates select="a:item | b:item"/>
</root>
</xsl:template>
<xsl:template match="a:item | b:item">
<xsl:element namespace="{namespace-uri(.)}" name="element">
<xsl:value-of select="a:name | b:name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:a="a" xmlns:b="b">
<element xmlns="a">a</element>
<element xmlns="b">b</element>
</root>
Thereby we construct XML with namespaces dynamically.
精彩评论