1

I have some XML

<p>Lorem ipsum dolor sit amet,<unclear reason="illegible"/> elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 
quis nostrud exercitation ullamco laboris <unclear reason="illegible"/> ex ea 
commodo consequat. Duis aute irure dolor in reprehenderit in 
voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
<unclear reason="illegible"/> non proident, sunt in culpa qui 
officia deserunt mollit anim id est laborum</p>

When I try and run

<xsl:value-of select="/p" disable-output-escaping="yes"/> 

it doesn't return the xml tags. How can I include the tags in value-of query?

hat I would like it to include the whole text with something to identify the unclear tags in the text.

4

1 回答 1

1

没错value-of,根据定义,an 元素是其所有后代文本节点的串联。您不能“在值查询中包含标签”,但您可以使用copy-of而不是value-of将整个p元素复制到输出,包括其子元素(文本节点和元素)

<xsl:copy-of select="/p" />

或者如果你想要元素的内容p而不是周围<p></p>标签(例如,如果你将内容插入到另一个元素中)那么

<xsl:copy-of select="/p/node()" />

如果您想将unclear元素转换为其他内容而不是按原样包含它们,那么您可能希望使用基于标识模板的转换

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- copy everything from input to output verbatim, except where
       a more specific template applies -->
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <!-- handle unclear elements differently -->
  <xsl:template match="unclear">
    <xsl:text>__UNCLEAR__</xsl:text>
  </xsl:template>
</xsl:stylesheet>

鉴于您的样本输入,这将产生

<p>Lorem ipsum dolor sit amet,__UNCLEAR__ elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 
quis nostrud exercitation ullamco laboris __UNCLEAR__ ex ea 
commodo consequat. Duis aute irure dolor in reprehenderit in 
voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
__UNCLEAR__ non proident, sunt in culpa qui 
officia deserunt mollit anim id est laborum</p>
于 2013-07-18T16:52:03.470 回答