我正在尝试将节点
<p class="p1">' , '<div class="disp-quote-p">' and div[@class='disp-quote-p']/following-sibling::*[1][self::p[@class='p1']]
包裹在单个 p
元素中。<root>
<p class="p">aa</p>
<p class="p1">Although:</p>
<div class="disp-quote-p">
<p class="p">We had seen.</p>
</div>
<p class="p1">This dot.</p>
<img src="a.png"/>
<box>box</box>
<p class="p">bb</p>
</root>
我正在尝试将节点包装在单个
p
元素中的代码,但此节点 <p class="p1">This dot.</p>
与 <div class="disp-quote-p">
节点一起包装。<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="p[@class='p1']">
<xsl:for-each-group select="current-group()" group-adjacent="self::p[@class='p1'] or self::div[@class='disp-quote-p']">
<xsl:choose>
<xsl:when test="self::p[@class='p1']">
<p><xsl:apply-templates select="node(), current-group() except ."/></p>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
网址链接:[http://xsltransform.net/eiov64R/1]
预期输出:-
<root>
<p class="p">aa</p>
<p>Although:<disp-quote><p class="p">We had seen.</p></disp-quote>This dot.</p>
<img src="a.png"/>
<box>box</box>
<p class="p">bb</p>
</root>
试试这个:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="*" group-adjacent="if(self::p[@class='p1'] or self::div) then -1 else position()">
<xsl:choose>
<xsl:when test="self::p[@class='p1'] or self::div">
<p><xsl:apply-templates select="current-group()"/></p>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="p[@class='p1']">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="div[@class='disp-quote-p']">
<xsl:element name="disp-quote">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>