根据第二个xml文件的属性值添加xml文件的属性值

时间:2016-09-23 17:26:20

标签: xml xslt

我需要在此文件中为子项添加credit_union_name的属性值。

<branches>
<branch branch_id="1" credit_union_id="1" branch_name="Sample Branch" phone_no="555-555-5555" fax_no="555-555-5555" toll_no="555-555-5555">
<address>
<suitenumber/>
<streetnumber>5030</streetnumber>
<streetsuffix/>
<streetname>51</streetname>
<streettype>ST</streettype>
<streetdirection/>
<city>CITY NAME</city>
<province>ON</province>
<postalcode>0P0 P0P</postalcode>
<longitude>0.0</longitude>
<latitude>0.0</latitude>
</address>
<hours>
<monopen>9:30</monopen>
<monclose>17:00</monclose>
<tueopen>9:30</tueopen>
<tueclose>17:00</tueclose>
<wedopen>9:30</wedopen>
<wedclose>17:00</wedclose>
<thursopen>9:30</thursopen>
<thursclose>17:00</thursclose>
<friopen>9:30</friopen>
<friclose>17:00</friclose>
<satopen>Closed</satopen>
<satclose>Closed</satclose>
<sunopen>Closed</sunopen>
<sunclose>Closed</sunclose>
</hours>
</branch>
</branches>

将上述文件的credit_union_id与下面文件的credit_union_id相匹配,并返回以下子项的name属性值。

<organizations>
<organization credit_union_id="1" name="Credit Union Sample" operatingas="Sample">
<licenseserviceaquirer>
<Acculink>True</Acculink>
<Interac>True</Interac>
<TheExchange>False</TheExchange>
<Plus>False</Plus>
<Cirrus>True</Cirrus>
<Maestro>True</Maestro>
<VisaCredit>False</VisaCredit>
<VisaDebit>False</VisaDebit>
<MasterCardCredit>False</MasterCardCredit>
<MasterCardDebit>True</MasterCardDebit>
</licenseserviceaquirer>
<services>
<CardServices>True</CardServices>
<CommercialLoans>True</CommercialLoans>
<ConsumerLoans>True</ConsumerLoans>
<DepositServices>True</DepositServices>
<AcculinkInBranch>True</AcculinkInBranch>
<InsuranceServices>True</InsuranceServices>
<InvestmentServices>False</InvestmentServices>
<LeasingServices>False</LeasingServices>
<MerchantServices>False</MerchantServices>
<PayrollDeduction>False</PayrollDeduction>
<RemoteBanking>True</RemoteBanking>
<VirtualBanking>True</VirtualBanking>
<ETransfer>True</ETransfer>
<CrossBorderDebit>False</CrossBorderDebit>
<InteracOnlinePayment>False</InteracOnlinePayment>
<AutomatedHotCardServices>True</AutomatedHotCardServices>
<DingFree>True</DingFree>
<Contactless>False</Contactless>
</services>
</organization>
</organizations>

我试图用XSLT样式表来做这件事。

感谢您阅读/帮助我的帖子!

1 个答案:

答案 0 :(得分:0)

在XSLT 2.0中,您可以轻松使用 keys 来解析对其他文档的交叉引用。这是一个最小化的例子:

XSLT 2.0

<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:strip-space elements="*"/>

<xsl:param name="path-to-organizations">organizations.xml</xsl:param>

<xsl:key name="org" match="organization" use="@credit_union_id" />

<xsl:template match="/branches">
    <xsl:copy>
        <xsl:for-each select="branch">
            <xsl:copy>
                <name>
                    <xsl:value-of select="@branch_name"/>
                </name>
                <organization>
                    <xsl:value-of select="key('org', @credit_union_id, document($path-to-organizations))/@name"/>
                </organization>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

当此样式表应用于您的第一个文件时,参数指向第二个文件,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<branches>
   <branch>
      <name>Sample Branch</name>
      <organization>Credit Union Sample</organization>
   </branch>
</branches>