如何在xslt中检查xml中是否存在元素

时间:2011-06-07 07:46:55

标签: xml xslt

这里是我的xml标签

<NewDataSet>
     <EETaxAmount>
          <WageID>TX01</WageID>
          <WageDesc>WithHoldingTax</WageDesc>
          <TaxAuthorityName>FED</TaxAuthorityName>
          <TaxAuthorityType>10</TaxAuthorityType>
          <Amount>931.25</Amount>
          <YTDAmount>982.92</YTDAmount>
     </EETaxAmount>
     <EETaxAmount>
          <WageID>TXS1</WageID>
          <WageDesc>WithHoldingTax(S)</WageDesc>
          <TaxAuthorityName>FED</TaxAuthorityName>
          <TaxAuthorityType>10</TaxAuthorityType>
          <Amount>50.00</Amount>
          <YTDAmount>50.00</YTDAmount>
     </EETaxAmount>
</NewDataSet>`

现在我的问题是我必须将值asoldingtax显示为withholdingtax(R)如果我在数据集中有withholdingtax(s)asholdingtax我的xsl如下所示

<xsl:for-each select="//NewDataSet/EETaxAmount">
  <xsl:sort select="TaxAuthorityType"/>
  <tr>
    <td class="txt5">
      <xsl:value-of select="WageDesc"/>
    </td>
    <td class="txt5">
      <xsl:value-of select="TaxAuthorityName"/>
    </td>
    <td class="txt_currency">
      <xsl:choose>
        <xsl:when test="Amount >=0 and Amount &lt;=1">
          <xsl:value-of select="format-number(Amount,'0.00')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="format-number(Amount, '###,###.00')"/>
        </xsl:otherwise>
      </xsl:choose>
    </td>
    <td class="txt_currency">
      <xsl:choose>
        <xsl:when test="YTDAmount >=0 and YTDAmount &lt;1">
          <xsl:value-of select="format-number(YTDAmount,'0.00')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="format-number(YTDAmount, '###,###.00')"/>
        </xsl:otherwise>
      </xsl:choose>
    </td>
  </tr>
</xsl:for-each>

4 个答案:

答案 0 :(得分:1)

<xsl:choose>
<xsl:when test="WageDesc[text() = 'WithHoldingTax(S)']">
withholdingtax(R)
</xsl:when>
<xsl:otherwise>
withholdingtax 
</xsl:otherwise>
</xsl:choose>

答案 1 :(得分:1)

在匹配WageDesc的模板中使用:

  <xsl:value-of select=
  "concat(., substring('(R)',
                       4 - 3*(/*/*/WageDesc = 'WithHoldingTax(S)')
                       )
          )"/>

请注意:在这种情况下,很多情况下都不需要XSLT条件指令(例如<xsl:choose><xsl:if><xsl:when>)。< / p>

避免纠结逻辑的另一种简洁方法是使用两个独立的模板,每个模板匹配两个条件中的一个:{/ p>

WageDesc

答案 2 :(得分:0)

<xsl:for-each select="NewDataSet/*">
  <xsl:if test="WageDesc[text() = 'WithHoldingTax(S)']"><xsl:value-of select="//WageDesc[text() = 'WithHoldingTax(R)']/text()" />  
  </xsl:if>  
</xsl:for-each>  

我怀疑内容节点在哪里,所以我选择// - 也许你应该使用更多合格的XPath

麦克

答案 3 :(得分:0)

按以下方式尝试

<xsl:choose>
<xsl:when test="count(//NewDataSet/EETaxAmount[WageDesc ='WithHoldingTax(S)'])>0 and WageDesc='WithHoldingTax'">
<xsl:value-of select="WageDesc"/>(R)
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="WageDesc"/>
</xsl:otherwise>
</xsl:choose>