-2

これは、xslt を生成するために必要な xml データです。

<root>
    <entry id="1">
         <headword>go</headword>
         <example>I <hw>go</hw> to school.</example>
    </entry>
    <entry id="2">
         <headword>come</headword>
         <example>I <verb>came</verb> back home.</example>           
    </entry>

次のような html を作成したいと思います。

<html>
     <body>
          <div class="entry" id="1">
                <span class="headword">go</span>
                <span class="example">I <span class="hw">go</span> to school.</span>
          </div>
          <div class="entry" id="2">
                <span class="headword">comeo</span>
                <span class="example">I <span class="hw">came</span> back home.</span>
          </div>
     </body>
</html>

これは私のxsltです:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="root/entry">
                    <div class="entry">
                        <span class="headword">
                            <xsl:value-of select="headword"/>
                        </span>
                        <span class="example">
                            <xsl:value-of select="example"/>
                        </span>
                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

属性「id」と要素「hw」の値を変換する方法がわかりません。

4

1 に答える 1

1

これを試してみてください。class="hw"サンプル出力の2番目はタイプミスでありclass="verb"、それが理にかなっている唯一の可能性であるため、タイプミスであると想定しています。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <xsl:apply-templates select="root/entry" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="entry">
    <div class="entry" id="{@id}">
      <xsl:apply-templates select="*" mode="entryContents" />
    </div>
  </xsl:template>

  <xsl:template match="*" mode="entryContents">
    <span class="{local-name()}">
      <xsl:apply-templates select="node()" mode="entryContents" />
    </span>
  </xsl:template>
</xsl:stylesheet>
于 2013-01-21T09:34:07.830 に答える