[Home] [By Thread] [By Date] [Recent Entries]

Subject: Re: Process an element once
From: "David Carlisle d.p.carlisle@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 16 Oct 2026 22:11:44 -0000
you are just missing a template to drop the x that you have copied under p
add

    <xsl:template match="x[preceding-sibling::*[1][self::p]]"/>

and you get

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <x>not a child of p</x>
   <p>1</p>
   <p>2</p>
   <p>
      <x>child of p</x>3</p>

   <p>4</p>
</root>

although I would ave expected to prcess the child of p before pulling in
the x so the text stayed in original order

so

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0" expand-text="yes">

    <xsl:output indent="yes"/>

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

    <xsl:template match="p">
        <xsl:copy>
          <xsl:apply-templates/>
 <xsl:apply-templates select="following-sibling::*[1][self::x]"
mode="child-x"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="x" mode="child-x">
        <xsl:copy><xsl:apply-templates/></xsl:copy>
    </xsl:template>

    <xsl:template match="x[not(preceding-sibling::*[1][self::p])]">
        <xsl:copy><xsl:apply-templates/></xsl:copy>
    </xsl:template>
    <xsl:template match="x[preceding-sibling::*[1][self::p]]"/>


</xsl:stylesheet>


producing


<?xml version="1.0" encoding="UTF-8"?>
<root>
   <x>not a child of p</x>
   <p>1</p>
   <p>2</p>
   <p>3<x>child of p</x>
   </p>

   <p>4</p>
</root>

David

Current Thread
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member