繁体   English   中英

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

[英]Remove elements in between two specific (HTML) tags in 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>标记。 <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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM