如何通过属性值去除元素中的内容?
How to remove the content with element by using the attribute value?
我想通过使用该属性值删除这两个段落,我尝试了以下操作,但它出现在输出中,
我的输入 XML 是:
<chapter outputclass="chapter-Body">
<body class="- chapter/body ">
<p class="- chapter/p ">why now?</p>
<p class="- chapter/p " outputclass="Image_Size">Banner</p>
<fig>
<image href="3.jpg" outputclass="Fig-Image_Ref"/>
</fig>
<p class="- chapter/p ">But for others, it may not be.</p>
<image ref="4.png" outputclass="Image_Ref"/>
<p class="- chapter/p " outputclass="Image_Size">Small</p>
<p class="- chapter/p ">As result</p>
</body>
</chapter>
我使用的 XSL:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="chapter[@outputclass='chapter-Body']">
<body>
<text>
<text_top><p><xsl:value-of select="body/fig/preceding-sibling::p[1]"/></p>
</text_top>
<text_bottom><p><xsl:value-of select="body/fig/following-sibling::p[1]"/></p>
<p><xsl:value-of select="body/fig/following-sibling::p[2]"/></p>
</text_bottom>
</text>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="p[@outputclass='Image_Size']"/>
</xsl:stylesheet>
XML 我得到的输出为:
<body>
<text>
<text_top>
<p>Banner</p>
</text_top>
<text_bottom>
<p>But for others, it may not be.</p>
<p>Small</p>
</text_bottom>
</text>
</body>
但我希望是这样的:
<body>
<text>
<text_top>
<p>why now?</p>
</text_top>
<text_bottom>
<p>But for others, it may not be.</p>
<p>As result</p>
</text_bottom>
</text>
</body>
我使用了那个特定两个参数的应用模板,但它正在输出中。如何使用相同的 XSLT 解决此问题?
preceding-sibling 轴的顺序是相反的文档顺序,因此当您执行 body/fig/preceding-sibling::p[1]
时,这将获得紧接在 fig
元素之前的 p
。你之前需要那个,所以你应该做 body/fig/preceding-sibling::p[2]
。
同样,您需要 fig
之后的第一个和第三个 p
元素,因此您也应该相应地调整索引。请注意,您当前匹配 p[@outputclass='Image_Size']
的模板根本不会被使用,因为 XSLT 中没有任何内容可以对 xsl:apply-templates
到 select 它们进行
试试这个 XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="chapter[@outputclass='chapter-Body']">
<body>
<text>
<text_top>
<p><xsl:value-of select="body/fig/preceding-sibling::p[2]"/></p>
</text_top>
<text_bottom>
<p><xsl:value-of select="body/fig/following-sibling::p[1]"/></p>
<p><xsl:value-of select="body/fig/following-sibling::p[3]"/></p>
</text_bottom>
</text>
</body>
</xsl:template>
</xsl:stylesheet>
话虽如此,看起来您要实现的目标是 select fig
前后的所有 p
元素,具有输出类的元素除外"Image_Size".
如果是这样,试试这个更通用的 XSLT。请注意现在如何使用匹配 p[@outputclass='Image_Size']
的模板,因为第一个模板中的 xsl:apply-templates
。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="chapter[@outputclass='chapter-Body']">
<body>
<text>
<text_top>
<xsl:apply-templates select="body/fig/preceding-sibling::p" />
</text_top>
<text_bottom>
<xsl:apply-templates select="body/fig/following-sibling::p" />
</text_bottom>
</text>
</body>
</xsl:template>
<xsl:template match="p[@outputclass='Image_Size']"/>
<xsl:template match="p">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>
我想通过使用该属性值删除这两个段落,我尝试了以下操作,但它出现在输出中,
我的输入 XML 是:
<chapter outputclass="chapter-Body">
<body class="- chapter/body ">
<p class="- chapter/p ">why now?</p>
<p class="- chapter/p " outputclass="Image_Size">Banner</p>
<fig>
<image href="3.jpg" outputclass="Fig-Image_Ref"/>
</fig>
<p class="- chapter/p ">But for others, it may not be.</p>
<image ref="4.png" outputclass="Image_Ref"/>
<p class="- chapter/p " outputclass="Image_Size">Small</p>
<p class="- chapter/p ">As result</p>
</body>
</chapter>
我使用的 XSL:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="chapter[@outputclass='chapter-Body']">
<body>
<text>
<text_top><p><xsl:value-of select="body/fig/preceding-sibling::p[1]"/></p>
</text_top>
<text_bottom><p><xsl:value-of select="body/fig/following-sibling::p[1]"/></p>
<p><xsl:value-of select="body/fig/following-sibling::p[2]"/></p>
</text_bottom>
</text>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="p[@outputclass='Image_Size']"/>
</xsl:stylesheet>
XML 我得到的输出为:
<body>
<text>
<text_top>
<p>Banner</p>
</text_top>
<text_bottom>
<p>But for others, it may not be.</p>
<p>Small</p>
</text_bottom>
</text>
</body>
但我希望是这样的:
<body>
<text>
<text_top>
<p>why now?</p>
</text_top>
<text_bottom>
<p>But for others, it may not be.</p>
<p>As result</p>
</text_bottom>
</text>
</body>
我使用了那个特定两个参数的应用模板,但它正在输出中。如何使用相同的 XSLT 解决此问题?
preceding-sibling 轴的顺序是相反的文档顺序,因此当您执行 body/fig/preceding-sibling::p[1]
时,这将获得紧接在 fig
元素之前的 p
。你之前需要那个,所以你应该做 body/fig/preceding-sibling::p[2]
。
同样,您需要 fig
之后的第一个和第三个 p
元素,因此您也应该相应地调整索引。请注意,您当前匹配 p[@outputclass='Image_Size']
的模板根本不会被使用,因为 XSLT 中没有任何内容可以对 xsl:apply-templates
到 select 它们进行
试试这个 XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="chapter[@outputclass='chapter-Body']">
<body>
<text>
<text_top>
<p><xsl:value-of select="body/fig/preceding-sibling::p[2]"/></p>
</text_top>
<text_bottom>
<p><xsl:value-of select="body/fig/following-sibling::p[1]"/></p>
<p><xsl:value-of select="body/fig/following-sibling::p[3]"/></p>
</text_bottom>
</text>
</body>
</xsl:template>
</xsl:stylesheet>
话虽如此,看起来您要实现的目标是 select fig
前后的所有 p
元素,具有输出类的元素除外"Image_Size".
如果是这样,试试这个更通用的 XSLT。请注意现在如何使用匹配 p[@outputclass='Image_Size']
的模板,因为第一个模板中的 xsl:apply-templates
。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="chapter[@outputclass='chapter-Body']">
<body>
<text>
<text_top>
<xsl:apply-templates select="body/fig/preceding-sibling::p" />
</text_top>
<text_bottom>
<xsl:apply-templates select="body/fig/following-sibling::p" />
</text_bottom>
</text>
</body>
</xsl:template>
<xsl:template match="p[@outputclass='Image_Size']"/>
<xsl:template match="p">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>