0

我阅读了很多文章,但没有找到对我的问题有决定性的帮助。

我有一个 XML 文档,我对其应用 xslt 以获取 csv 文件作为输出。我向我的 xsl 转换发送一个参数以过滤目标节点以应用模板。

xml 文档看起来像这样(我删除了一些无用的节点以便理解):

<GetMOTransactionsResponse xmlns="http://www.exane.com/pott" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.exane.com/pott PoTTMOTransaction.xsd">
<MOTransaction>
    <Transaction VersionNumber="2" TradeDate="2013-11-20">
        <TransactionId Type="Risque">32164597</TransactionId>
        <InternalTransaction Type="Switch">
            <BookCounterparty>
                <Id Type="Risque">94</Id>
            </BookCounterparty>
        </InternalTransaction>
        <SalesPerson>
            <Id Type="Risque">-1</Id>
        </SalesPerson>
    </Transaction>
    <GrossPrice>58.92</GrossPrice>
    <MOAccount Account="TO1E" />
    <Entity>0021</Entity>
</MOTransaction>
<MOTransaction>
    <Transaction VersionNumber="1" TradeDate="2013-11-20">
        <TransactionId Type="Risque">32164598</TransactionId>
        <SalesPerson>
            <Id Type="Risque">-1</Id>
        </SalesPerson>
    </Transaction>
    <GrossPrice>58.92</GrossPrice>
    <MOAccount Account="TO3E" />
    <Entity>0021</Entity>
</MOTransaction>
</GetMOTransactionsResponse>

我的 xslt 在下面(对不起,它很长,我写的比它实际上更简单):

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:pott="http://www.exane.com/pott">

  <xsl:output method="text" omit-xml-declaration="no" indent="no" />

  <xsl:param name="instrumentalSystem"></xsl:param>

  <xsl:template name="abs">
    <xsl:param name="n" />
    <xsl:choose>
      <xsl:when test="$n = 0">
        <xsl:text>0</xsl:text>
      </xsl:when>
      <xsl:when test="$n &gt; 0">
        <xsl:value-of select="format-number($n, '#')" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="format-number(0 - $n, '#')" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="outputFormat">
    <!--Declaration of variables-->
    <xsl:variable name="GrossPrice" select="pott:GrossPrice" />
    <xsl:variable name="TransactionId" select="pott:Transaction/pott:TransactionId[@Type='Risque']" />
    <xsl:variable name="VersionNumber" select="pott:Transaction/@VersionNumber" />

    <!--Set tags values-->
    <xsl:value-of select="$Entity" />
    <xsl:text>;</xsl:text>
    <xsl:value-of select="concat('0000000', pott:MOAccount/@Account) "/>
    <xsl:text>;</xsl:text>

    <xsl:text>;</xsl:text>
    <xsl:value-of select="$TransactionId" />
    <xsl:text>;</xsl:text>
    <xsl:value-of select="$VersionNumber" />
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>
  <xsl:template match="/">
    <xsl:choose>
      <!-- BB -->
      <xsl:when test="$instrumentalSystem = 'BB'">
        <!--xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction/pott:Transaction[pott:InternalTransaction]"-->
        <xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction/pott:Transaction[pott:InternalTransaction]">
          <xsl:call-template name="outputFormat"></xsl:call-template>
        </xsl:for-each>
      </xsl:when>

      <!-- CP -->
      <xsl:when test="$instrumentalSystem = 'CP'">
        <xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction/pott:Transaction[not(pott:InternalTransaction)]">
          <xsl:call-template name="outputFormat"></xsl:call-template>
        </xsl:for-each>
      </xsl:when>

      <xsl:otherwise>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

</xsl:stylesheet>

如果参数 = BB,我想选择具有包含InternalTransaction节点的子事务的MOTransaction节点。

如果参数 = CP,我想选择没有包含InternalTransaction节点的子事务的MOTransaction节点

当我写 pott:GetMOTransactionsResponse/pott:MOTransaction/pott:Transaction[pott:InternalTransaction] 时,我得到了事务节点而不是 MOTransaction 节点. 如果有人可以帮助我。

我希望清楚,否则我可以提供更多信息。

4

1 回答 1

0

查看xsl:for-each语句之一,您正在执行此操作

<xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction/pott:Transaction[pott:InternalTransaction]">

您说要选择MOTransaction元素,但实际上是在选择子Transaction元素。为了匹配您需要的逻辑,它应该是这样的

<xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction[pott:Transaction[pott:InternalTransaction]]">

事实上,这也应该工作

<xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction[pott:Transaction/pott:InternalTransaction]">

同样,对于第二个语句(在参数为“CP”的情况下),它可能看起来像这样

<xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction[pott:Transaction[not(pott:InternalTransaction)]]">

或者,它可能看起来像这样

<xsl:for-each select="pott:GetMOTransactionsResponse/pott:MOTransaction[not(pott:Transaction/pott:InternalTransaction)]">

但是它们并不完全相同,因为第一个将仅包括具有Transaction子元素的MOTransaction元素,而第二个将包括根本没有任何Transaction子元素的MOTransaction

顺便说一句,您实际上并不需要在这里使用xsl:for-eachxsl:call-template。使用模板匹配可能会更好。

首先,尝试将命名模板<xsl:template name="outputFormat">更改为此

<xsl:template match="pott:MOTransaction">

然后,您可以重新编写将xsl:for-eachxsl:call-template合并到单个xsl:apply-templates调用中。

<xsl:apply-template select="pott:GetMOTransactionsResponse/pott:MOTransaction[pott:Transaction/pott:InternalTransaction]" />
于 2013-11-22T13:45:04.487 回答