Separating XSLT on a node child

I have the following XML code

<para>Lorem ipsum <link>dolor</link> sit amet</para>

which i want to convert to

<para>Lorem ipsum </para><link>dolor</link><para> sit amet</para>

In other words: I would like to split the para element at the position where there is a link element. Are there any clues?

+3
source share
2 answers

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="no"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="para/text()">
  <para><xsl:copy-of select="."/></para>
 </xsl:template>
</xsl:stylesheet>

when applied to the provided XML document :

<para>Lorem ipsum <link>dolor</link> sit amet</para>

creates the desired, correct result :

<para>Lorem ipsum </para><link>dolor</link><para> sit amet</para>

Please note :

  • Using an identification rule to copy all nodes as is.

  • Overriding an identification rule with templates to process only specific nodes

  • , 1. 2.

+3

:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[1]"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="para">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[1]"/>
        </xsl:copy>
        <xsl:apply-templates select="link" mode="copy"/>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="para/link"/>
    <xsl:template match="para/link" mode="copy">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[1]"/>
        </xsl:copy>
        <para>
            <xsl:apply-templates select="following-sibling::node()[1]"/>
        </para>
    </xsl:template>
</xsl:stylesheet>

:

<para>Lorem ipsum </para><link>dolor</link><para> sit amet</para>

: .

: :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[1]"/>
        </xsl:copy>
        <xsl:apply-templates select="self::para/link" mode="copy"/>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="para/link"/>
    <xsl:template match="para/link" mode="copy">
        <xsl:call-template name="identity"/>
    </xsl:template>
    <xsl:template match="node()[preceding-sibling::node()[1]
                                    /self::link/parent::para]">
        <para>
            <xsl:call-template name="identity"/>
        </para>
    </xsl:template>
</xsl:stylesheet>
0

Source: https://habr.com/ru/post/1775652/


All Articles