-2

次のようなxml要素があります。

<book>
    <English color="blue" author="hasan" />
    <English color="red" author="david" />
</book>

xslt を使用して反復処理し、以下のような出力を生成することは可能ですか?

<book>
    <English color="yellow" author="hally" />
    <English color="pink" author="gufoo" />
</book>

これが私が試しているものです。

<xsl:template match = /book> 
  <xsl:for-each select "./English"> 
    <xsl:if test="@color = '"yellow"'"> 
    <English color="yellow"/> 
    <xsl:if test="@color = '"red"'"> 
    <English color="pink"/> 
  </xsl:for-each> 
 </xsl-template>
4

1 に答える 1

0

次のスタイルシートを試してください。xsl:for-each 要素を削除したので、この方法の方が簡単だと思います。また、XSL のような宣言型言語で foreach ループを使用するのは適切ではないように思えます。私はそれらを命令型言語に残すことを好みます。

このような結果を得るには、さまざまな方法があります。これを修正して少し実験するのに時間がかかるはずです。演習として、if ステートメントを削除し、テンプレートと述語を使用して同様の結果を達成することを試みることができます。そうする前に、おそらく XSL に関するいくつかのチュートリアルを読む必要があります。

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

  <!-- Copy every element or attribute encountered 
       and find matching templates for its attributes
       and child elements 
   -->
  <xsl:template match="@*|*">
    <xsl:copy>
      <xsl:apply-templates select="@*|*"></xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!-- For every attribute named "color" that has a value of red or blue, 
  follow the conditions defined in if blocks.
  Notice that the specified color attributes will not be copied according
  to the template above as the one selected is always the last
  matching one in your XSL.
  This way both the "author" attributes and "color" attributes with values
  different than red and blue will be matched by the other template.
  The dot "." means the currently processed node (usually element or attribute) 
  -->
  <xsl:template match="@color[. = 'blue' or . = 'red']">
   <xsl:attribute name="color">
     <xsl:if test=". = 'blue'">yellow</xsl:if>
     <xsl:if test=". = 'red'">pink</xsl:if>
   </xsl:attribute>
  </xsl:template>

于 2012-06-08T18:29:25.810 に答える