【问题标题】:If p sub element appears close before the sub element and open after the sub element using xslt如果 p 子元素出现在子元素之前关闭并使用 xslt 在子元素之后打开
【发布时间】:2021-05-10 06:02:43
【问题描述】:

我有一个输入 xml:

<root>
<p>text 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>

需要输出为每个子元素关闭出现的关闭p 输出为:

<root>
<p>text 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 xslt-2.0


    【解决方案1】:

    类似这样的:

    <?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="/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 文本包含粗体或斜体字符样式,则使用您的模板忽略
    • @User515:好的,我看到您更改了示例。它们不会被忽略,但不会以您现在所期望的方式出现。我会看看
    • @User515 ,这是你想要的吗?
    • 嗨@Siebe Jongebloed 您的答案适用于该场景,但如果在输入中我有类似'

      text 21

      ' 它的输出是'text 2

      21

      ' 请建议将输出作为 '

      text 21

      ' 如果可能的话
    • 这里发布的另一个问题link
    猜你喜欢
    • 2014-01-16
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多