Sam
Sam

Reputation: 841

If start with p element processing-instruction node then how to move processing-instruction position[1] before p element? - XSLT

If start with p element processing-instruction node then how to move processing-instruction position[1] before p element? - XSLT
Input

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

Expected output

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

Upvotes: 0

Views: 36

Answers (1)

Sam
Sam

Reputation: 841

Solved

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

Upvotes: 0

Related Questions