首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xslt基于属性值组合2个XML文档

如何使用xslt基于属性值组合2个XML文档
EN

Stack Overflow用户
提问于 2014-08-15 22:10:40
回答 1查看 66关注 0票数 1

我试图使用XSLT从XML文件1中获取customer-no属性,并使用它查找XML文件2中的计费地址。并将这两个文件的数据组合起来创建XML文件3。有人能帮我吗?提前谢谢你。

XML文件1

代码语言:javascript
复制
    <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

代码语言:javascript
复制
    <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> 

结果XMLFile3

代码语言:javascript
复制
    <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的一部分

代码语言:javascript
复制
    <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>  
EN

回答 1

Stack Overflow用户

发布于 2014-08-18 16:57:14

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

给定此输入XML文件:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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> 

以下样式表:

XSLT1.0

代码语言:javascript
复制
<?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>

将返回:

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25334923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档