1

xslt を使用して、コーヒー項目の数をループする必要がありますが、20 回に制限する必要があります。10 個のコーヒー アイテムがある場合、20 回未満で 10 回ループする必要があります。20点以上のコーヒーはお受けできません。ループ コーヒー アイテムの後、変換された xml の CoffeeList ノードに追加します。

また、コーヒーアイテムに価値がない場合は無視してください。xsltファイルで実装する方法。あなたの助けに感謝します。

XML:

<Action>
<Coffee1> hello 1 </Coffee1>
<Coffee2> hello 2</Coffee2>
<Coffee3> </Coffee3>
<Coffee4> hello 4</Coffee4>

<Amount1>1.2000</Amount1>
<Amount2>2.0000</Amount2>
<Amount3>1.2100</Amount3>
<Amount4>2.0000</Amount4>
</Action>

出力:

    <CoffeeList>
      <Coffee coffeeCode="hello 1" amount="1.2000" />
      <Coffee coffeeCode="hello 2 " amount="2.0000" />
      <Coffee coffeeCode="hello 4" amount="1.2100" />
    </CoffeeList>

XSLT: - 出力が必要なため、これを実装する方法がわかりません。

  <xsl:for-each select="Action">
  <xsl:sort select="Coffee" data-type="string" />  
  <xsl:if test="position() &lt; 20">
        <xsl:value-of select="Coffee"/>
  </xsl:if>
  </xsl:for-each>
4

1 に答える 1

2

通常、XSLT ではループを回避する必要があります。

代わりに、ノードを選択してテンプレートを適用します。

<xsl:template match="Action">
  <CoffeeList>
    <xsl:apply-templates select="*[
      starts-with(name(), 'Coffee') and normalize-space(.) != ''
    ]" />
  </CoffeeList>
</xsl:template>

<xsl:template match="*[starts-with(name(), 'Coffee')]">
  <xsl:variable name="myNumber" select="substring-after(name(), 'Coffee')" />
  <xsl:variable name="amountName" select="concat('Amount', $myNumber)" />
  <xsl:variable name="amount" select="../*[name() = $amountName]" />

  <Coffee coffeeCode="{normalize-space(.)}" amount="{$amount}" />
</xsl:template>

http://www.xmlplayground.com/E0eXFsを参照してください。

<xsl:if test="position() &lt; 21">たとえば、2 番目のテンプレートに を追加して、それ以上出力されないようにすることができます。


<Coffee1>また、一般に、 やのような「番号付き」要素は避けたいと考えてい<Coffee2>ます。これらの要素が同じ概念 (コーヒーなど) を表す場合は、すべて同じ名前にする必要があります。

于 2013-11-13T09:32:10.093 に答える