如果 p 子元素出现在子元素之前关闭并在使用 xslt 包含子字符元素子元素之后打开 [英] If p sub element appears close before the sub element and open after the including child character elements sub element using xslt
本文介绍了如果 p 子元素出现在子元素之前关闭并在使用 xslt 包含子字符元素子元素之后打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个输入xml:
<root>
<p>text <i>2</i> 1</p>
<p>text <i>2</i>
<disp-quote>
<p>text <b>3</b></p>
</disp-quote>
text <b>4</b>
<disp-quote>
<p>text 5</p>
</disp-quote>
text 6</p>
</root>
我使用了以下 XSLT 转换:
I have used the below XSLT transform:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root/p" >
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/root/p/i|root/p/b"/>
<xsl:template match="/root/p/text()[normalize-space()!='']">
<p>
<xsl:copy-of select="preceding-sibling::node()[1][self::i|self::b]"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::node()[1][self::i|self::b]"/>
</p>
</xsl:template>
</xsl:stylesheet>
将输出作为分割的 p 元素和重复的字符元素和文本,但我需要在单个 p 元素文本中:
Getting output as divided p element and repeated character element and text but I need in single p element text:
<?xml version="1.0" encoding="UTF-8"?><root>
<p>text <i>2</i></p><p><i>2</i>1</p>
<p>text <i>2</i></p>
<p><disp-quote>
<p>text <b>3</b></p>
</disp-quote>
text <b>4</b></p>
<p><disp-quote>
<p>text 5</p>
</disp-quote>
text 6</p>
</root>
需要输出作为每个子元素关闭出现的关闭 p输出为:
Need output as close p for each sub element closing appears output as:
<root>
<p>text <i>2</i> 1</p>
<p>text <i>2</i></p>
<blockquote>
<p>text <b>3</b></p>
</blockquote>
<p>text <i>4</i></p>
<blockquote>
<p>text 5</p>
</blockquote>
<p>text 6</p>
</root>
提前致谢
推荐答案
这样做:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="disp-quote">
<blockquote>
<xsl:apply-templates/>
</blockquote>
</xsl:template>
<xsl:template match="/root/p[disp-quote]" >
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/root/p[disp-quote]/i|root/p[disp-quote]/b" />
<xsl:template match="/root/p[disp-quote]/text()[normalize-space()!='']">
<p>
<xsl:copy-of select="preceding-sibling::node()[1][self::i|self::b]"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::node()[1][self::i|self::b]"/>
</p>
</xsl:template>
</xsl:stylesheet>
这篇关于如果 p 子元素出现在子元素之前关闭并在使用 xslt 包含子字符元素子元素之后打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文