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

时间:2019-01-23 11:06:38

标签: xml xslt-1.0

我有一个 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>标记。 <p>之后的两个<h6>SOURCE DESCRIPTOR</h6>标签

我尝试过类似的

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

通过删除除必需项之外的所有<p>而得到相反的结果。

2 个答案:

答案 0 :(得分:0)

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

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>

答案 1 :(得分:0)

由于需要在给定的输入源(已经是某种转换的输出)上应用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" />