J'ai trouvé plusieurs éléments différents (par exemple, span, italic) dans l'élément p. span éléments continus fusionnés en un seul élément, mais comment gérer plusieurs éléments italic continus.
Entrée XML

<root>
    <p>Paragraph <span>a</span><span>b</span><span>c</span><span>d</span> Under Continuing <span>Court</span> Jurisdiction <italic>a</italic><italic>b</italic><italic>c</italic></p>
    <p>Paragraph <italic>a</italic><italic>b</italic><italic>c</italic><italic>d</italic> Under Continuing Court <span>Jurisdiction</span></p>
</root>

Code XSLT

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

<xsl:template match="p">
    <xsl:copy>
        <xsl:for-each-group select="node()" group-adjacent=". instance of element(span), element(italic)">
            <xsl:choose>
                <xsl:when test="current-grouping-key()[1]">
                    <span>
                        <xsl:apply-templates select="current-group()/node()"/>
                    </span>
                </xsl:when>
                <xsl:when test="current-grouping-key()[2]">
                    <italic>
                        <xsl:apply-templates select="current-group()/node()"/>
                    </italic>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

Sortie attendue

<root>
    <p>Paragraph <span>abcd</span> Under Continuing <span>Court</span> Jurisdiction <italic>abc</italic></p>
    <p>Paragraph <italic>abcd</italic> Under Continuing Court <span>Jurisdiction</span></p>
</root>
0
Sam 14 sept. 2020 à 05:40

1 réponse

Meilleure réponse

Vous pouvez modifier votre deuxième modèle en utilisant les local-name() des éléments comme ceci :

<xsl:template match="p">
    <xsl:copy>
        <xsl:for-each-group select="node()" group-adjacent="local-name()='span' or local-name()='italic'">
            <xsl:choose>
                <xsl:when test="current-grouping-key()">
                    <xsl:copy>
                        <xsl:apply-templates select="current-group()/node()"/>
                    </xsl:copy>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

La sortie est

<root>
    <p>
        Paragraph <span>abcd</span>
        Under Continuing <span>Court</span>
        Jurisdiction <italic>abc</italic>
    </p>
    <p>
        Paragraph <italic>abcd</italic>
        Under Continuing Court <span>Jurisdiction</span>
    </p>
</root>

La sortie peut être formatée différemment.

1
zx485 14 sept. 2020 à 03:52