【问题标题】:Delete html tags from text during xslt transformation在 xslt 转换期间从文本中删除 html 标记
【发布时间】:2017-12-08 10:11:34
【问题描述】:

我正在做 xslt 转换,但我遇到了问题。我必须转换的文本有一些 html 标签,但我有 <> 而不是 。我想从

更改所有标签的内容

ici-import/issue/article/languageVersion/abstract

并将其转化为新元素:abstract,删除html标签

这是我试图实现的代码:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="ici-import/issue/article/languageVersion/">
        <xsl:variable name="StripHTML">
            <![CDATA[&lt;\s*\w.*?&gt;|&lt;\s*/\s*\w\s*.*?&gt;]]>
        </xsl:variable>
        <xsl:analyze-string select="abstract" regex="{$StripHTML}">
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>
</xsl:stylesheet>

我目前的 xslt:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <ici-import>
            <journal issn="2299-3711" />
            <issue>
                <xsl:for-each select="issue/section">
                    <xsl:for-each select="article">
                        <article>
                            <languageVersion>
                                <xsl:if test="abstract != ''">
                                    <abstract>
                                        <xsl:value-of select="abstract"/>
                                    </abstract>
                                </xsl:if>
                            </languageVersion>
                        </article>
                    </xsl:for-each>
                </xsl:for-each>
            </issue>
        </ici-import>
    </xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 那么您的输入看起来如何?您使用哪种 XSLT 处理器?转义的 HTML 是可解析为 XHTML 还是使用 HTML 语法(如缺少结束标记或未引用的属性值)?

标签: xml xslt xslt-2.0


【解决方案1】:

如果您转义的内容是 XML 片段,那么在 XSLT 3(受 Saxon 9.8 HE 或 Altova 2017 和 2018 支持)中,您可以简单地解析内容并将其推送到仅复制文本的模式。

这是一个例子:

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <data><![CDATA[<section id="s1">
    <h2>Test</h2>
    <p style="font-size: 120%">This is some text.</p>
</section>
<section>
  <h2>Another test</h2>
  <p>This is some text.</p>
  </section>]]></data>
</root>

由 Saxon 9.8 HE 和 XSLT 转换

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="xs math map array"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:mode name="strip" on-no-match="text-only-copy"/>

  <xsl:template match="data">
      <xsl:copy>
          <xsl:apply-templates select="parse-xml-fragment(.)" mode="strip"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

进入

<?xml version="1.0" encoding="UTF-8"?><root>
    <data>
    Test
    This is some text.


  Another test
  This is some text.
  </data>
</root>

如果内容不是 XML 而是 HTML,那么您可以导入用 XSLT 2 编写的 David Carlisle 的 HTML 解析器的本地副本,并使用它来解析内容并通过仅复制文本的模式将其推送:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    xmlns:htmlparser="data:,dpc"
    exclude-result-prefixes="xs math map array htmlparser"
    version="3.0">

  <!-- adjust to local copy of file -->
  <xsl:import href="https://github.com/davidcarlisle/web-xslt/raw/master/htmlparse/htmlparse.xsl"/>

  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:mode name="strip" on-no-match="text-only-copy"/>

  <xsl:template match="data">
      <xsl:copy>
          <xsl:apply-templates select="htmlparser:htmlparse(.)/node()" mode="strip"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

使用 Saxon 9.8 HE 进行转换

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <data><![CDATA[<section id=s1>
    <h2>Test</h2>
    <p style="font-size: 120%">This is some text.</p>
</section>
<section>
  <h2>Another test</h2>
  <p>This is some text.
  <p>This is some other text.
  </section>]]></data>
</root>

进入

<?xml version="1.0" encoding="UTF-8"?><root>
    <data>
    Test
    This is some text.


  Another test
  This is some text.
  This is some other text.
  </data>
</root>

当然,由于 HTML 解析器是用 XSLT 2 编写的,您也可以将它与任何 XSLT 2 处理器一起使用,您只需将 &lt;xsl:mode name="strip" on-no-match="text-only-copy"/&gt; 替换为

<xsl:template match="*" mode="strip">
  <xsl:apply-templates/>
</xsl:template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 2014-10-13
    • 2016-04-02
    • 2011-10-08
    • 2015-08-14
    相关资源
    最近更新 更多