[英]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.