删除 XSLT 1.0 中两个特定 (HTML) 标记之间的元素



我有一个XML输入源,如下所示:

                        <p>(U) This product may contain copyrighted material.</p>
                        <h6>BODY</h6>
                        <p>Today, located in Don Chedi District, Suphan Buri Province.</p>
                        <h6>SOURCE DESCRIPTOR</h6>
                        <p>From the Royal Thai by the private company.</p>
                        <p>This product may contain copyrighted material</p>
                        <h6>PRODUCT DESCRIPTION</h6>
                        <p>The body of this product is a translation of original foreign-language material.</p>
                        <p>From the Royal News section</p>

我需要的输出是:

                        <p>(U) This product may contain copyrighted material.</p>
                        <h6>BODY</h6>
                        <p>Today, located in Don Chedi District, Suphan Buri Province.</p>
                        <h6>PRODUCT DESCRIPTION</h6>
                        <p>The body of this product is a translation of original foreign-language material.</p>
                        <p>From the Royal News section</p>

我需要删除<h6>具有价值SOURCE DESCRIPTOR的元素,并且它都遵循<p>标签。<h6>SOURCE DESCRIPTOR</h6>后可以有两个以上的<p>标签

我尝试了类似的东西

<xsl:template match="p[following-sibling::*[self::h6='SOURCE DESCRIPTOR']]" />

它通过删除除所需<p>之外的所有给出相反的结果。

给定格式正确的输入,例如:

.XML

<root>
    <p>(U) This product may contain copyrighted material.</p>
    <h6>BODY</h6>
    <p>Today, located in Don Chedi District, Suphan Buri Province.</p>
    <h6>SOURCE DESCRIPTOR</h6>
    <p>From the Royal Thai by the private company.</p>
    <p>This product may contain copyrighted material</p>
    <h6>PRODUCT DESCRIPTION</h6>
    <p>The body of this product is a translation of original foreign-language material.</p>
    <p>From the Royal News section</p>
</root>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="p-by-h6" match="p" use="generate-id(preceding-sibling::h6[1])" />
<xsl:template match="/root">
    <xsl:copy>
        <xsl:copy-of select="key('p-by-h6', '')"/>
        <xsl:for-each select="h6[not(.='SOURCE DESCRIPTOR')]">
            <xsl:copy-of select="."/>
            <xsl:copy-of select="key('p-by-h6', generate-id())"/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

将返回:

结果

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <p>(U) This product may contain copyrighted material.</p>
  <h6>BODY</h6>
  <p>Today, located in Don Chedi District, Suphan Buri Province.</p>
  <h6>PRODUCT DESCRIPTION</h6>
  <p>The body of this product is a translation of original foreign-language material.</p>
  <p>From the Royal News section</p>
</root>

由于需要在给定的输入源上应用 XSLT(这已经是某些转换的输出),我像下面这样解决了它,这有助于完成给定的要求:

<xsl:template match="p['SOURCE DESCRIPTOR' = preceding-sibling::h6[1]]" mode="xhtml" xmlns="http://www.w3.org/1999/xhtml" />
<xsl:template match="h6[. = 'SOURCE DESCRIPTOR']" mode="xhtml" xmlns="http://www.w3.org/1999/xhtml" />

相关内容

  • 没有找到相关文章

最新更新