简体   繁体   中英

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

I am trying to use XSLT to take the customer-no attribute from XML file 1 and use it to look up the billing address in XML file 2. And combine data from both files to create XML file 3. Can anyone help me out? Thank you in advance.

XML file 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 file 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> 

Result XML File 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>

Part of my 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>  

This is still confusing: your input and output don't have root elements, and your XSLT attempt does not exactly match either.

Given this input XML file:

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

and another XML file, 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> 

the following stylesheet:

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>

will return:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM