Change in the rootnode
Old Input xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SalesOrder>
<SalesOrderHeader>
<OrderNumber>
<BuyerOrderNumber>3016</BuyerOrderNumber>
</OrderNumber>
</SalesOrderHeader>
<SalesOrderDetail>
<ListOfItemDetail>
<ItemDetail>
<BaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>00001</BuyerLineItemNum>
</LineItemNum>
</BaseItemDetail>
</ItemDetail>
<ItemDetail>
<BaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>00002</BuyerLineItemNum>
</LineItemNum>
</BaseItemDetail>
</ItemDetail>
</ListOfItemDetail>
</SalesOrderDetail>
</SalesOrder>
Old Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL开发者_StackOverflow中文版/Transform" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/SalesOrder">
<ns0:Z_SALES_ORDER xmlns:ns0="urn:sap-com:document:sap:rfc:functions" >
<SHEADER>
<xsl:call-template name="sHeader"/>
</SHEADER>
<SITEM>
<xsl:call-template name="sItem"/>
</SITEM>
</ns0:Z_SALES_ORDER>
</xsl:template>
<xsl:template name="sHeader">
<SNUMBER>
<xsl:value-of select="SalesOrderHeader/OrderNumber/BuyerOrderNumber "/>
</SNUMBER>
</xsl:template>
<xsl:template name="sItem">
<xsl:for-each select="descendant::ItemDetail">
<item>
<B_ITEM>
<xsl:value-of select="BaseItemDetail/LineItemNum/BuyerLineItemNum"/>
</B_ITEM>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Required Output:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Z_SALES_ORDER xmlns:ns0="urn:sap-com:document:sap:rfc:functions">
<SHEADER>
<SNUMBER>3016</SNUMBER>
</SHEADER>
<SITEM>
<item>
<B_ITEM>00001</B_ITEM>
</item>
<item>
<B_ITEM>00002</B_ITEM>
</item>
</SITEM>
</ns0:Z_SALES_ORDER>
Query: Now there is some change in the source xml. In the SalesOrder rootnode they pass other info as well. The input file is below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SalesOrder xmlns="abc:org.xcbl:schemas/xcbl/v4/xcbl4.xsd" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding">
<SalesOrderHeader>
<OrderNumber>
<BuyerOrderNumber>3016</BuyerOrderNumber>
</OrderNumber>
</SalesOrderHeader>
<SalesOrderDetail>
<ListOfItemDetail>
<ItemDetail>
<BaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>00001</BuyerLineItemNum>
</LineItemNum>
</BaseItemDetail>
</ItemDetail>
<ItemDetail>
<BaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>00002</BuyerLineItemNum>
</LineItemNum>
</BaseItemDetail>
</ItemDetail>
</ListOfItemDetail>
</SalesOrderDetail>
</SalesOrder>
Could you please tell me,how to get the same output which I was getting using the old input xml file.
EDIT Query: In the SalesOrder rootnode they pass other info as well. The input file is below. How to get the rootnode info using XSLT?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SalesOrder xmlns="abc:org.xcbl:schemas/xcbl/v4/xcbl4.xsd" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding">
<SalesOrderHeader>
<OrderNumber>
<BuyerOrderNumber>3016</BuyerOrderNumber>
</OrderNumber>
</SalesOrderHeader>
</SalesOrder>
Now that the new XML has a namespace you need to qualify your xpath with this namespace. Easiest is to use an alias:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:abc="abc:org.xcbl:schemas/xcbl/v4/xcbl4.xsd"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="abc xsl"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/abc:SalesOrder">
<ns0:Z_SALES_ORDER xmlns:ns0="urn:sap-com:document:sap:rfc:functions" >
<SHEADER>
<xsl:call-template name="sHeader"/>
</SHEADER>
<SITEM>
<xsl:call-template name="sItem"/>
</SITEM>
</ns0:Z_SALES_ORDER>
</xsl:template>
<xsl:template name="sHeader">
<SNUMBER>
<xsl:value-of select="abc:SalesOrderHeader/abc:OrderNumber/abc:BuyerOrderNumber "/>
</SNUMBER>
</xsl:template>
<xsl:template name="sItem">
<xsl:for-each select="descendant::abc:ItemDetail">
<item>
<B_ITEM>
<xsl:value-of select="abc:BaseItemDetail/abc:LineItemNum/abc:BuyerLineItemNum"/>
</B_ITEM>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Edit (Sara)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:abc="abc:org.xcbl:schemas/xcbl/v4/xcbl4.xsd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="abc xsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/abc:SalesOrder">
<ns0:Z_SALES_ORDER xmlns:ns0="urn:sap-com:document:sap:rfc:functions">
<SNUMBER>
<xsl:value-of select="abc:SalesOrderHeader/abc:OrderNumber/abc:BuyerOrderNumber "/>
</SNUMBER>
</ns0:Z_SALES_ORDER>
</xsl:template>
</xsl:stylesheet>
精彩评论