2

私はいくつかのXSLを練習していて、このXMLドキュメントを簡単な例として使用しています。

<?xml version="1.1" encoding="UTF-8"?>

<zoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="zoo.xsd" >
    <animals>
        <animal type="lion">
            <name>Zeus</name>
            <gender>M</gender>
            <eats>antelope</eats>
            <eats>monkey</eats>
        </animal>
        <animal type="monkey">
            <name>Fredo</name>
            <gender>M</gender>
            <eats>banana</eats>
            <iseatenby>lion</iseatenby>
        </animal>
        <animal type="lion">
            <name>Here</name>
            <gender>F</gender>
            <eats>antelope</eats>
            <eats>monkey</eats>
        </animal>
        <animal type="antelope">
            <name>Annie</name>
            <gender>F</gender>
            <eats>grass</eats>
            <iseatenby>lion</iseatenby>
        </animal>
        <animal type="elephant">
            <name>Moses</name>
            <gender>M</gender>
            <eats>leaves</eats>
        </animal>
    </animals>
</zoo>

XSLドキュメントを介していくつかの基本的な情報を取得できましたが、現在1つのことに固執しています。複数の結果がある場合、どうすればすべての結果を取得できますか?たとえば、私のドキュメントでは、一部の動物には複数の「食べる」要素があります。それらをコンマ区切りの文字列で表示したい。最終的には、各動物の要素を属性に変換し、それぞれに1つの属性を設定したいと思います。(前の例を使用すると、新しい動物要素のライオンの「食べる」属性は次のようになりますeats="antelope, monkey":)

誰かがXSLでこのようなことをどのように行うか説明してもらえますか?どんな助けでも大歓迎です。:)

4

2 に答える 2

2

完璧ではありませんが:)

今それを試してみてくださいそれが役立つことを願っています..私は各要素を属性として変換しています..

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="//animal">
    <xsl:copy>
      <!--copies all other elements as attributes-->
      <xsl:for-each select="*[name()!='eats']">
        <xsl:attribute name="{name()}">
          <xsl:apply-templates select = "text()"/>
        </xsl:attribute>
      </xsl:for-each> 

      <xsl:attribute name="eats">

        <!-- Go to <eats/> node -->
        <xsl:for-each select="eats">

          <!--insearts string ", " if it has preceding values already :) -->
          <xsl:if test="preceding-sibling::eats">
            <xsl:text>, </xsl:text>
          </xsl:if>

          <!--copies all the text :) -->
          <xsl:apply-templates select="text()"/>

        </xsl:for-each>
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
于 2012-01-30T05:01:56.013 に答える
1

これを使って:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="zoo/animals">
    <xsl:copy>
      <xsl:apply-templates select="animal"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="animal">
    <xsl:copy>
      <xsl:attribute name="eats">
        <xsl:for-each select="eats">
          <xsl:value-of select="."/>

          <xsl:if test="position() != last()">
            <xsl:text>,</xsl:text>
          </xsl:if>
        </xsl:for-each>
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

参照:

position関数は、式評価コンテキストからコンテキスト位置に等しい数値を返します。最後の関数は、式の評価コンテキストからコンテキストサイズに等しい数値を返します。

xsl:if現在のノードがコンテキスト内の最後のノードではないかどうかを確認します。その場合、出力します,

http://www.w3.org/TR/xpath/#function-last

于 2012-01-30T04:53:55.143 に答える