簡體   English   中英

如何使用xslt根據屬性值組合2個XML文檔

[英]How to use xslt to combine 2 XML documents based on an attribute value

我正在嘗試使用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>  

這仍然令人困惑:您的輸入和輸出沒有根元素,並且您的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文件customer.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM