提取除xslt中的注释数据之外的注释数据

时间:2017-10-17 07:04:05

标签: xslt

输入xml就是这样,

<section level="2" counter="yes">
<title id="c1_3"><!--1.3--> Reeve’s Prosthesis (1972)</title>
<figure counter="yes" id="f1_1">
<legend><para><!--<emph type="bold">Fig. 1.1</emph>--> Reeve’s prosthesis. (Reproduced with permission from Reeves B, Jobbins B, Dowson D, Wright V. A Total Shoulder Endo-Prosthesis. Eng Med 1972;1(3):64–67.)</para></legend>
<para><!--<inline-figure xlink:href="images/copy.jpg"/>--></para>
</figure>
</section>

输出应该是,

<section level="2" counter="yes">
<title id="c1_3">1.3 Reeve’s Prosthesis (1972)</title>
<figure counter="yes" id="f1_1">
<legend><para><emph type="bold">Fig. 1.1</emph> Reeve’s prosthesis. (Reproduced with permission from Reeves B, Jobbins B, Dowson D, Wright V. A Total Shoulder Endo-Prosthesis. Eng Med 1972;1(3):64–67.)</para></legend>
<para><!--<inline-figure xlink:href="images/copy.jpg"/>--></para>
</figure>
</section>

我的xslt写得像这样,

<xsl:template match="document//comment()">
<xsl:choose>
<xsl:when test="ancestor::para | ancestor::caption | ancestor::section | ancestor::document">
<xsl:text disable-output-escaping="yes">&lt;comment&gt;</xsl:text>
<xsl:variable name="commentText0"><xsl:copy-of select="replace(normalize-space(.),'
',' ')"/></xsl:variable>
<xsl:variable name="commentText2"><xsl:value-of select="replace($commentText0, 'Fig([.]) ', 'Fig. ')" disable-output-escaping="yes"/></xsl:variable>
<xsl:variable name="commentText3"><xsl:value-of select="replace($commentText2, 'Table ', 'Table ')" disable-output-escaping="yes"/></xsl:variable>
<xsl:value-of select="replace($commentText3, 'Formula ', 'Formula ')" disable-output-escaping="yes"/>
<xsl:text disable-output-escaping="yes">&lt;/comment&gt;</xsl:text>
</xsl:when>
<xsl:when test="comment[contains(.,'inline-figure')]">
<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>                
<xsl:apply-templates select="node() | @*"/>                
<xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>

我想提取除<inline-figure>元素之外的评论内容。能否指导我如何为它编写代码。

2 个答案:

答案 0 :(得分:0)

你的XSLT中有很多东西似乎与你声明的要求没有任何关系。假设你有一个允许disable-output-escaping工作的XSLT环境,你应该能够做到:

<xsl:template match="comment()">
  <xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>

<xsl:template match="comment()[contains(., 'inline-figure')]">
  <xsl:copy/>
</xsl:template>

我无法看到你的其他逻辑想要实现的目标。

另请注意,disable-output-escaping通常仅在从转换中写入最终序列化输出时才有效,而不是在写入变量时。

答案 1 :(得分:0)

假设XSLT 3.0(由Saxon 9.8或Altova 2017/2018支持)您可以使用

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="document//comment()[not(contains(., 'inline-figure'))]">
   <xsl:copy-of select="parse-xml-fragment(.)"/>
</xsl:template>