Hi All,
I have this XML:
<root>
<x>not a child of p</x>
<p>1</p>
<p>2</p>
<p>3</p>
<x>child of p</x>
<p>4</p>
</root>
If an <x> element immediately follows a <p>, I want to make it a child of
the <p>:
<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>
I thought I could do this with a mode but I am still getting the text of the
<x> after the <p> that contains the child <x>:
<root>
<x>not a child of p</x>
<p>1</p>
<p>2</p>
<p>
<x>child of p</x>3</p>
child of p
<p>4</p>
</root>
Here is my stylesheet:
<?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:choose>
<xsl:when test="following-sibling::*[1][self::x]">
<xsl:apply-templates
select="following-sibling::*[1][self::x]" mode="child-x"/>
</xsl:when>
</xsl:choose>
<xsl:apply-templates/>
</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:stylesheet>
Any help will be appreciated. Thank you very much.
Rick Quatro
|