0

我有类似的xml代码:

<body>Text Here1.
</body>
<body><Title>Title</Title>Text Here2.
</body>
<body>Text Here3.
</body>

我在我的 XSLT 中使用以下代码:

<xsl:when test="@name='body'">
<p> 
<xsl:value-of select='normalize-space(node())'/>
</p>
</xsl:when>

在第二个节点中忽略该子元素的最佳机制是什么,或者可能在节点内对其应用特殊格式(假设我想加粗该文本)?

谢谢

4

1 回答 1

1

当使用 XSLT 处理层次结构时,通常使用应用模板,它允许您递归地遍历 XML 输入。下面的示例将body使用元素将文本封装在元素内paragraph,并将文本封装在Title元素内的元素内bold。所有其他元素将被忽略。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>
  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="body">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>
  <xsl:template match="Title">
    <b>
      <xsl:apply-templates />
    </b>
  </xsl:template>
  <xsl:template match="text()">
    <xsl:value-of select='normalize-space(.)'/>
  </xsl:template>
</xsl:stylesheet>
于 2012-05-07T21:49:16.773 回答