如果 p 子元素出现在子元素之前关闭并在包含子字符元素子元素之后打开,则使用 xslt

If p sub element appears close before the sub element and open after the including child character elements sub element using 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 转换:

<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 元素文本:

<?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 输出为:

<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>