更改rootnode

时间:2011-10-12 05:56:05

标签: xslt xslt-2.0

  

旧输入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>
  

旧代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/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>
  

必需输出:

<?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>
  

查询:现在源xml发生了一些变化。在SalesOrder中   rootnode他们也传递其他信息。输入文件在下面。

<?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>

请告诉我,如何使用旧的输入xml文件获得相同的输出。

修改 查询:在SalesOrder根节点中,它们也传递其他信息。输入文件如下。如何使用XSLT获取rootnode信息?

<?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>

1 个答案:

答案 0 :(得分:1)

既然新XML有一个命名空间,您需要使用此命名空间限定xpath。最简单的方法是使用别名:

<?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>

编辑(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>