0

我有一个 TEI XML 文档,其内容如下:

<said who="#Bernard">“I see a ring,” said Bernard, “hanging above
me.  It quivers and hangs in a loop of light.”&lt;/said>

<said who="#Susan">“I see a slab of pale yellow,” said Susan,
spreading away until it meets a purple stripe.”&lt;/said>

我想像这样输出 XHTML:

<p class="Bernard">“I see a ring,” said Bernard, “hanging above
me.  It quivers and hangs in a loop of light.”&lt;/p>

<p class="Susan">“I see a slab of pale yellow,” said Susan,
spreading away until it meets a purple stripe.”&lt;/p>

将属性值映射到 XHTML 类的最佳方法是什么?

4

1 回答 1

2

样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="1.0" encoding="utf-8"/>

  <!-- Match root node -->
  <xsl:template match="/">
    <html>
      <body>
        <!-- Apply child nodes -->
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <!-- Match <said> elements... -->
  <xsl:template match="said">
    <!-- ...and transform into <p> -->
    <p>
      <!-- Apply attributes and other child nodes -->
      <xsl:apply-templates select="@* | node()"/>
    </p>
  </xsl:template>

  <!-- Match @who attributes... -->
  <xsl:template match="@who">
    <!-- ...and transform into @class attributes -->
    <xsl:attribute name="class">
      <!-- Omit the hash mark -->
      <xsl:value-of select="substring(., 2)"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

输入

<waves>
  <said who="#Bernard">“I see a ring,” said Bernard, “hanging above
  me.  It quivers and hangs in a loop of light.”&lt;/said>

  <said who="#Susan">“I see a slab of pale yellow,” said Susan,
  spreading away until it meets a purple stripe.”&lt;/said>
</waves>

输出

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
  <p class="Bernard">“I see a ring,” said Bernard, “hanging above
  me.  It quivers and hangs in a loop of light.”&lt;/p>

  <p class="Susan">“I see a slab of pale yellow,” said Susan,
  spreading away until it meets a purple stripe.”&lt;/p>
</body></html>
于 2013-04-11T20:13:59.507 回答