如果从p元素开始处理指令节点,那么如何将处理指令位置[1]移动到p元素之前?-XSLT



如果从p元素开始处理指令节点,那么如何将处理指令位置[1]移动到p元素之前?-XSLT
输入

<root>
<p><?page 1?>aaaa bbbb <?page 2?>cccccc</p>
<p>aaaa <?page 3?>bbbb <?page 4?>ccccc</p>
</root>

预期输出

<root>
<?page 1?><p>aaaa bbbb <?page 2?>cccccc</p>
<p>aaaa <?page 3?>bbbb <?page 4?>ccccc</p>
</root>

XSLT

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p[count(processing-instruction()) > 1]">
<xsl:apply-templates select="processing-instruction()[1]"/>
<xsl:copy>
<xsl:apply-templates select="node() except processing-instruction()[1]"/>
</xsl:copy>
</xsl:template>

已解决

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p[count(processing-instruction()) > 1 and child::processing-instruction()[not(preceding-sibling::node())]]">
<xsl:apply-templates select="processing-instruction()[1]"/>
<xsl:copy>
<xsl:apply-templates select="node() except processing-instruction()[1]"/>
</xsl:copy>
</xsl:template>