【问题标题】:XSLT - Uncomment the element based on tile contentXSLT - 根据平铺内容取消注释元素
【发布时间】:2017-10-31 05:46:49
【问题描述】:

输入 XML

<section counter="no" type="index">
<title><target id="page417"/>Index</title>
<para>Page numbers in italics represent applications. Page numbers followed by <emph type="italic">f</emph> and <emph type="italic">t</emph> indicate figures and tables, respectively.</para>
<section counter="no">
<title>A</title>
....................
<litem><para>Ammonia/ammonium ion (NH<sub>3</sub>/<!--<inline-figure xlink:href="images/9781604063172_in108.jpg"/>-->)</para>
.....................
</section>
</section>

输出应该是,

<section counter="no" type="index">
<title><target id="page417"/>Index</title>
<para>Page numbers in italics represent applications. Page numbers followed by <emph type="italic">f</emph> and <emph type="italic">t</emph> indicate figures and tables, respectively.</para>
<section counter="no">
<title>A</title>
....................
<litem><para>Ammonia/ammonium ion (NH<sub>3</sub>/<inline-figure xlink:href="images/9781604063172_in108.jpg"/>)</para>
.....................
</section>

我的 xslt 就像,

<xsl:template match="section[@type='index']">
    <xsl:if test="contains(.,comment())">
        <xsl:copy>
            <xsl:value-of select="." disable-output-escaping="yes"/>
        </xsl:copy>    
    </xsl:if>
</xsl:template>

我们需要根据标题内容删除评论。使用上述 xslt 时,从节点中提取所有内容。输出中只有值可用。请您指导我们。

【问题讨论】:

  • 我们需要删除基于title 内容的评论 - 你能在这里分享一下要求吗?您还需要identity template 来复制所有内容并将其保留在输出中。这将确保所有节点都在输出中可用。
  • 是的..我想删除基于标题的评论。每当标题为“索引”并且 cmets 出现在该指定部分中时,我们需要删除取消注释它。
  • 在这种情况下,我们需要注释整个“索引”部分。之前,我们使用这个 xslt 来评论整个部分。

标签: xml xslt


【解决方案1】:

您对“取消注释元素”问题的描述表明您并未考虑此处的实际数据模型。没有要取消注释的元素:有一个包含文本的注释,其中包含 XML 标记,可以对其进行解析以创建一个元素。

处理此问题的最佳方法是提取评论文本并将其提交给 XML 解析器。在 XSLT 3.0 中,您可以通过调用 parse-xml() 或 parse-xml-fragment() 函数来做到这一点。在早期版本中,您将需要使用扩展功能(可以从您的供应商处获得,或者您可以自己编写。

disable-output-escaping 在某些情况下可以解决问题,但它是一个粗略的工具,因为 (a) 它仅在某些情况下有效,并且 (b) 在输出内容之前您不能对内容进行太多操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    相关资源
    最近更新 更多