1

我对 XSLT 比较陌生,但想要执行我认为是元素之间相对简单的匹配以获得另一个元素。

这是 XML 的一个片段。版本 = 1.0,输出为文本(将 xml 转换为文本)。

<Accounts>
              <Account>
                 <Id>273228MD301</Id>
                 <EPIProductCode>IPP4D3</EPIProductCode>
                 <Name>Mr John Smith</Name>
                 <Status>Open</Status>
                 <Owners>
                    <Owner>
                       <Id>273228M</Id>
                    </Owner>
                 </Owners>
                 <Advisers>
                    <Adviser>
                       <Id>286666</Id>
                       <PrimaryAdviser>true</PrimaryAdviser>
                    </Adviser>
                 </Advisers>
                 <Delete>false</Delete>
                 <LastModified>2012-06-08T15:19:19</LastModified>
              </Account>
              <Account>
                <Id>273228MD399</Id>
                <EPIProductCode>IPAAA</EPIProductCode>
                <Name>Sir Leslie Patterson</Name>
                <Status>Open</Status>
                <Owners>
                    <Owner>
                        <Id>2732299</Id>
                    </Owner>
                </Owners>
                <Advisers>
                   <Adviser>
                        <Id>286666</Id>
                <PrimaryAdviser>true</PrimaryAdviser>
                </Adviser>
                </Advisers>
                <Delete>false</Delete>
                <LastModified>2012-06-08T15:19:19</LastModified>
              </Account>
              <Account>
                <Id>273228MD999</Id>
                <EPIProductCode>IPYYY</EPIProductCode>
                <Name>Dame Edna</Name>
                <Status>Open</Status>
                <Owners>
                   <Owner>
                      <Id>27322YY</Id>
                   </Owner>
                </Owners>
                <Advisers>
                   <Adviser>
                      <Id>286666</Id>
                      <PrimaryAdviser>true</PrimaryAdviser>
                   </Adviser>
                </Advisers>
                <Delete>false</Delete>
                <LastModified>2012-06-08T15:19:19</LastModified>
                 </Account>
                </Accounts>
<InvestmentHoldingBalances>
              <HoldingBalance>
                 <AccountId>273228MD399</AccountId>
                 <InvestmentCode>TEST123</InvestmentCode>
                 <Exchange>FND</Exchange>
                 <UnitBalance>
                    <Settled Currency="AUD">0</Settled>
                    <Pending Currency="AUD">0</Pending>
                    <AsAtDate>2012-06-08T15:19:34</AsAtDate>
                 </UnitBalance>
                 <LastModified>2012-05-16T00:00:00</LastModified>
              </HoldingBalance>
              <HoldingBalance>
                 <AccountId>273228MD301</AccountId>
                 <InvestmentCode>0114AU</InvestmentCode>
                 <Exchange>FND</Exchange>
                 <UnitBalance>
                    <Settled Currency="AUD">0</Settled>
                    <Pending Currency="AUD">0</Pending>
                    <AsAtDate>2012-06-08T15:19:34</AsAtDate>
                 </UnitBalance>
                 <LastModified>2012-05-16T00:00:00</LastModified>
              </HoldingBalance>
              <HoldingBalance>
                 <AccountId>273228MD301</AccountId>
                 <InvestmentCode>0016AU</InvestmentCode>
                 <Exchange>FND</Exchange>
                 <UnitBalance>
                    <Settled Currency="AUD">0</Settled>
                    <Pending Currency="AUD">0</Pending>
                    <AsAtDate>2012-06-08T15:19:34</AsAtDate>
                 </UnitBalance>
                 <LastModified>2012-05-16T00:00:00</LastModified>
              </HoldingBalance>
              <HoldingBalance>
                 <AccountId>273228MD301</AccountId>
                 <InvestmentCode>0277AU</InvestmentCode>
                 <Exchange>FND</Exchange>
                 <UnitBalance>
                    <Settled Currency="AUD">0</Settled>
                    <Pending Currency="AUD">0</Pending>
                    <AsAtDate>2012-06-08T15:19:34</AsAtDate>
                 </UnitBalance>
                 <LastModified>2012-05-15T00:00:00</LastModified>
              </HoldingBalance>
              <HoldingBalance>
                 <AccountId>273228MD999</AccountId>
                 <InvestmentCode>TD0155</InvestmentCode>
                 <Exchange>FND</Exchange>
                 <UnitBalance>
                    <Settled Currency="AUD">0</Settled>
                    <Pending Currency="AUD">0</Pending>
                    <AsAtDate>2012-06-08T15:19:34</AsAtDate>
                 </UnitBalance>
                 <LastModified>2012-05-21T00:00:00</LastModified>
              </HoldingBalance>
           </InvestmentHoldingBalances>

我要做的是将节点//Accounts/Account/Id 与//InvestmentHoldingBalances/HoldingBalance/AccountId 匹配,当匹配时,获取属于该帐户ID 的相应//Owners/Owner/Id。当我进行匹配时,我得到的结果是所有行的第一个 //Owners/Owner/Id,而不是单个匹配的行。这是我的 xslt;

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" indent="yes" />

<xsl:template match="/">
   <xsl:apply-templates />
</xsl:template>

<!--  Create Header Record -->
<xsl:template match="xxxxxx">
<!-- Some other xslt here to extract header info -->
 <xsl:apply-templates select="InvestmentHoldingBalances/HoldingBalance" />  
</xsl:template> 

<!-- Create HoldingBalance Records -->
<xsl:template match="HoldingBalance">
<xsl:value-of select="AccountId" />
<xsl:text>","</xsl:text>
<xsl:value-of select="InvestmentCode" />
<xsl:text>","</xsl:text>
<xsl:value-of select="Exchange" />
<xsl:text>","</xsl:text>
  <xsl:if test="../../Accounts/Account/Id=AccountId">
  <xsl:value-of select="../../Accounts/Account/Owners/Owner/Id" />
 </xsl:if>   
<xsl:text>"</xsl:text>
<xsl:text disable-output-escaping="yes">&#10;</xsl:text> 
</xsl:template>

</xsl:stylesheet>

输出的是每一行的相同Owner Id(即'273228M'最后一列),而不是根据Account Id匹配的Owner Id;

273228MD399","TEST123","FND","273228M"
273228MD301","0114AU","FND","273228M"
273228MD301","0016AU","FND","273228M"
273228MD301","0277AU","FND","273228M"
273228MD999","TD0155","FND","273228M"

我追求的结果是这样的;

273228MD399","TEST123","FND","2732299"
273228MD301","0114AU","FND","273228M"
273228MD301","0016AU","FND","273228M"
273228MD301","0277AU","FND","273228M"
273228MD999","TD0155","FND","27322YY"

感谢您的任何建议。

4

1 回答 1

0

基本问题在于

<xsl:value-of select="../../Accounts/Account/Owners/Owner/Id"/>

select 表达式选择整个文档中的所有所有者 ID,而不考虑帐户 ID,当您要求value-of一组多个节点时,规范将结果定义为文档中集合中第一个节点的值命令。

您需要以某种方式将集合限制为仅匹配的帐户,最简单的方法是

<xsl:value-of select="../../Accounts/Account[Id = current()/AccountId]/Owners/Owner/Id"/>

但是在样式表的顶层定义一个可能更有效(直接放在xsl:output元素之后)

<xsl:key name="accountById" match="Account" use="Id"/>

value-of然后您可以在使用中提取正确的帐户

<xsl:value-of select="key('accountById', AccountId)/Owners/Owner/Id"/>

无论哪种方式,您实际上都不需要if周围的value-of,因为在没有帐户与当前帐户匹配的情况下,AccountId您将要求value-of一个空节点集,根据定义,它是空字符串。

disable-output-escaping最后,您在使用时永远不需要<xsl:output method="text"/>-在<xsl:text>&#10;</xsl:text>这里也可以正常工作。

于 2013-05-07T08:26:56.580 回答