babysimon: (Default)
[personal profile] babysimon
For some reason XSL just won't stick in my brain, so I have to ask my friends list.

If I have a document which contains AAA nodes and I want to transform them to BBB nodes I can do something like:
	<xsl:template match="AAA">
		<BBB><xsl:apply-templates/></BBB>
	</xsl:template>


But suppose I want to replace every AAA immediately followed by a CCC (throwing the CCC away)? Something like
	<xsl:template match="AAA/following-sibling::CCC[1]">
		<BBB><xsl:apply-templates/></BBB>
	</xsl:template>

is my guess, but then I'm told "Only child:: and attribute:: axes are allowed in match patterns!"

Anyone got any clues? I'm trying to pretend this isn't just directed at [livejournal.com profile] lozette...

(no subject)

Date: 2004-09-21 12:11 pm (UTC)
From: [identity profile] lozette.livejournal.com
I'd probably do something like:

xsl:template match="AAA"
xsl:if test="following-sibling::*[1][name() = 'CCC']"
...do something
/
/

Not very elegant, though, and totally untested!!

If that doesn't work or isn't right/convenient let me know.

(no subject)

Date: 2004-09-21 12:31 pm (UTC)
From: [identity profile] lozette.livejournal.com
OK, so you want to match all the CCCs and only throw them away if they directly follow an AAA (otherwise, do something with them)?

In that case, maybe:

xsl:template match="AAA"
xsl:if test="following-sibling::*[1][name() = 'CCC']"
...do something
/
/

xsl:template match="CCC"
xsl:if test="not(preceding-sibling::*[1][name() = 'AAA')"
...do something
/
/

Or, you could do it sort of backwards...

<xsl:template match="CCC">
<xsl:choose>
<xsl:when test="preceding-sibling::*[1][name() = AAA">
<xsl:apply-templates select="preceding-sibling::AAA[1]" mode="theOneBefore"/>
</xsl:when>
<xsl:otherwise>
...do what you want to do with all CCCs that don't have a preceding AAA
</xsl:otherwise>
</xsl:template>

<xsl:template match="AAA" mode="theOneBefore">
...do what you want to do with all AAAs that have a CCC after them
</xsl:template>

<xsl:template match="AAA"/>
...empty template to match all other AAAs, assuming you have any. Or you can do something in here if you want to.

Again, no idea if this does exactly what you want cos I'm still a bit fuzzy on the requirement ;-)

(no subject)

Date: 2004-09-21 01:41 pm (UTC)
From: [identity profile] ciphergoth.livejournal.com
You want to put the "following-sibling" stuff inside the square brackets representing the test...

April 2017

S M T W T F S
      1
2345678
910 1112131415
16171819202122
23242526272829
30      

Style Credit

Expand Cut Tags

No cut tags
Page generated Apr. 20th, 2026 09:15 pm
Powered by Dreamwidth Studios