2016-04-11 1 views
0

説明:Multipile「ではない」condtions(XSLT)

私は属性と入ってくるフィールドの値をチェックする必要がある要件を持っている、との両方がNULLでない場合、私は中にこれらの値を移入出力。

問題:値の一つでも渡されたとき、私は出力を取得しています

(属性や実際のフィールド値のいずれか)。入力XMLの「両方」のために 'と'の条件が必要なので、そうではありません。

私は2つの入力XMLを持っている:

最初:

<a> 
    <b Ccy="INR">999.000</b> 
</a> 

秒:

<v> 
    <z Ccy="USD">9999.000</z> 
</v> 

コードを使用:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    exclude-result-prefixes="fo"> 
    <xsl:output encoding="UTF-8" 
       version="1.0" 
       method="xml" 
       indent="yes"/> 
    <xsl:template match="/"> 
    <xsl:variable name="ccy1" 
     select="./*[local-name()='a'] 
       /*[local-name()='b'] 
       /@*[local-name()='Ccy']"/> 
    <xsl:variable name="field1" 
     select="./*[local-name()='a'] 
       /*[local-name()='b']"/> 
    <xsl:variable name="ccy2" 
     select="./*[local-name()='v'] 
       /*[local-name()='z'] 
       /@*[local-name()='Ccy']"/> 
    <xsl:variable name="field2" 
     select="./*[local-name()='v'] 
       /*[local-name()='z']"/> 
    <xsl:if test="not($ccy1 = '') 
        and not($field1 = '') 
        or not($ccy2 = '') 
        and not($field2 = '')"> 
     <output Ccy="AAA"> 
     <xsl:choose> 
      <xsl:when test="$ccy1 and $field1"> 
      <xsl:attribute name="Ccy"> 
       <xsl:value-of select="$ccy1"/> 
      </xsl:attribute> 
      <xsl:value-of select="$field1"/> 
      </xsl:when> 
      <xsl:when test="$ccy2 and $field2"> 
      <xsl:attribute name="Ccy"> 
       <xsl:value-of select="$ccy2"/> 
      </xsl:attribute> 
      <xsl:value-of select="$field2"/> 
      </xsl:when> 
     </xsl:choose> 
     </output> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 
+1

なぜあなたは '* [ローカル名()= '']'のような恐ろしいハックを使うのでしょうか? –

+0

これはXPathを取得する通常の方法ですか? –

+0

いいえ、そうではありません。要素が名前空間にある場合は、 'a'または--ns1:a'(名前空間を宣言し、' ns1'接頭辞にバインドした後)を使用します。 –

答えて

0

をあなたのアプローチに問題があります空のノード集合がnであること空の文字列でなくても文字列に等しい。したがって、最初の例では、実際の入力ノードが空であるかどうかに関係なく、式not($ccy2 = '') and not($field2 = '')"true()と評価されます。

代わりに試してみてください:地球上の

<xsl:if test="string($ccy1) and string($field1) or string($ccy2) and string($field2)"> 
+0

迅速な対応をありがとう –