如何使用xslt根据属性值组合2个XML文档

时间:2014-08-15 22:10:40

标签: xml xslt

我正在尝试使用XSLT从XML文件1获取customer-no属性,并使用它来查找XML文件2中的帐单地址。并将两个文件中的数据组合起来创建XML文件3.任何人都可以帮助我出来吗?提前谢谢。

XML文件1

    <product-list list-id="bcrEMiaagQaewaaacUi0Q04Nee">
        <owner customer-no="DEVSBX00000207">
            <email>person@place.com</email>
        </owner>
        <type>custom_1</type>
        <public>false</public>
    </product-list>     
    <product-list list-id="bc2sAiaagQ2kYaaacWC2USApxn">
        <owner customer-no="STGSBX00000405">
            <email>person1@place.com</email>
        </owner>
        <type>custom_1</type>
        <public>false</public>
    </product-list>             

XML文件2

    <customers>
        <customerID customer-no="DEVSBX00000207">
        <billing-address>
            <first-name>Herman</first-name>
            <last-name>Munster</last-name>
            <address1>1313 mockingbird lane</address1>
            <city>Los Santos</city>
            <postal-code>99999-6772</postal-code>
            <state-code>CA</state-code>
            <country-code>US</country-code>
            <phone>999-555-1212</phone>
        </billing-address>
        <payment-card>
            <card-type>Visa</card-type>
        </payment-card>
    </customerID>
    <customerID customer-no="STGSBX00000405">
        <billing-address>
            <first-name>Greg</first-name>
            <last-name>Brady</last-name>
            <address1>123 main st</address1>
            <city>burbank</city>
            <postal-code>11111-3456</postal-code>
            <state-code>CA</state-code>
            <country-code>US</country-code>
            <phone>999-555-1212</phone>
        </billing-address>
        <payment-card>
            <card-type>Visa</card-type>
        </payment-card>
      </customerID>
    <customers> 

结果XML文件3

    <OrderDetail>
      <email>person1@place.com</email>
      <type>custom_1</type>
      <public>false</public>
      <Addresses>
        <Address>
          <firstname>Herman</first-name>
          <lastname>Munster</last-name>
          <address1>1313 mockingbird ln</address1>
          <city>Los Santos</city>
          <postalcode>99999-6772</postal-code>
          <state>CA</state-code>
          <country>US</country-code>
          <phone1>999-555-1212</phone>
         </Address>
        </Addresses>
    </OrderDetail>
    <OrderDetail>
      <email>person1@place.com</email>
      <type>custom_1</type>
      <public>false</public>
      <Addresses>
        <Address>
          <firstname>Greg</first-name>
          <lastname>Brady</last-name>
          <address1>123 main st</address1>
          <city>burbank</city>
          <postalcode>11111-3456</postal-code>
          <state>CA</state-code>
          <country>US</country-code>
          <phone1>999-555-1212</phone>
         </Address>
        </Addresses>
    </OrderDetail>

我的XSLT的一部分

    <xsl:template match="/">
        <GlobalMerchant>
        <xsl:for-each select="product-lists/product-list/items/product-item">
            <xsl:variable name="cnum" select="../../owner/@customer-no"/>

            {other transforms from xml file 1 removed}

            <Address>
                <ClientID>53510</ClientID>
                <xsl:apply-templates select="document('xmlfile2.xml')//*[@customer-no=$cnum]" mode="billing-info"/>                
            </Address>
        </xsl:for-each>
        </GlobalMerchant>
    </xsl:template>     

    <xsl:template match="@*|node()" mode="billing-info">
        <postalcode>
            <xsl:value-of select="document('xmlfile2.xml')/customers/customerID/billing-address/postal-code" />
        </postalcode>
    </xsl:template>  

1 个答案:

答案 0 :(得分:0)

这仍然令人困惑:您的输入和输出没有根元素,而您的XSLT尝试也不完全匹配。

鉴于此输入XML 文件:

<product-lists>
    <product-list list-id="bcrEMiaagQaewaaacUi0Q04Nee">
        <owner customer-no="DEVSBX00000207">
            <email>person@place.com</email>
        </owner>
        <type>custom_1</type>
        <public>false</public>
    </product-list>     
    <product-list list-id="bc2sAiaagQ2kYaaacWC2USApxn">
        <owner customer-no="STGSBX00000405">
            <email>person1@place.com</email>
        </owner>
        <type>custom_1</type>
        <public>false</public>
    </product-list>            
</product-lists>

和另一个XML文件 customers.xml

<customers>
    <customerID customer-no="DEVSBX00000207">
        <billing-address>
            <first-name>Herman</first-name>
            <last-name>Munster</last-name>
            <address1>1313 mockingbird lane</address1>
            <city>Los Santos</city>
            <postal-code>99999-6772</postal-code>
            <state-code>CA</state-code>
            <country-code>US</country-code>
            <phone>999-555-1212</phone>
        </billing-address>
        <payment-card>
            <card-type>Visa</card-type>
        </payment-card>
    </customerID>
    <customerID customer-no="STGSBX00000405">
        <billing-address>
            <first-name>Greg</first-name>
            <last-name>Brady</last-name>
            <address1>123 main st</address1>
            <city>burbank</city>
            <postal-code>11111-3456</postal-code>
            <state-code>CA</state-code>
            <country-code>US</country-code>
            <phone>999-555-1212</phone>
        </billing-address>
        <payment-card>
            <card-type>Visa</card-type>
        </payment-card>
      </customerID>
</customers> 

以下样式表:

XSLT 1.0

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.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="/">
    <GlobalMerchant>
        <xsl:for-each select="product-lists/product-list">
            <OrderDetail>
                <xsl:copy-of select="owner/email | type | public"/>
                <Address>
                    <xsl:copy-of select="document('customers.xml')/customers/customerID[@customer-no=current()/owner/@customer-no]/billing-address/*"/>
                </Address>
            </OrderDetail>
        </xsl:for-each>
    </GlobalMerchant>
</xsl:template>     

</xsl:stylesheet>

将返回:

<?xml version="1.0" encoding="UTF-8"?>
<GlobalMerchant>
   <OrderDetail>
      <email>person@place.com</email>
      <type>custom_1</type>
      <public>false</public>
      <Address>
         <first-name>Herman</first-name>
         <last-name>Munster</last-name>
         <address1>1313 mockingbird lane</address1>
         <city>Los Santos</city>
         <postal-code>99999-6772</postal-code>
         <state-code>CA</state-code>
         <country-code>US</country-code>
         <phone>999-555-1212</phone>
      </Address>
   </OrderDetail>
   <OrderDetail>
      <email>person1@place.com</email>
      <type>custom_1</type>
      <public>false</public>
      <Address>
         <first-name>Greg</first-name>
         <last-name>Brady</last-name>
         <address1>123 main st</address1>
         <city>burbank</city>
         <postal-code>11111-3456</postal-code>
         <state-code>CA</state-code>
         <country-code>US</country-code>
         <phone>999-555-1212</phone>
      </Address>
   </OrderDetail>
</GlobalMerchant>