xsl apply-templates select=not(...)

  • Thread starter Cyrille \cns\ Szymanski
  • Start date
C

Cyrille \cns\ Szymanski

Hello,

I have a XML document which is typically :

<course>
<section>
<title>Foo</title>
There comes the text
</section>
<section>
<title>Foo</title>
There comes the text
</section>
</course>

I'd like to indent the "There comes the text" part, but not the title.
This is what I tried, but unfortunately it doesn't work :

<xsl:template match="course/section">
<xsl:apply-templates select="title"/>
<ul>
<xsl:apply-templates select="*[not(title)]"/>
</ul>
</xsl:template>

Could someone give me advice on this. I'd like to avoid the for-if
combination if possible.

Thanks in advance,
 
P

Patrick TJ McPhee

% I have a XML document which is typically :
%
% <course>
% <section>
% <title>Foo</title>
% There comes the text
% </section>

From a design perspective, I think you're better off if you make
section have element content (that is, you wrap all your text in
additional elements). For one thing, if that's valid, then this must
also be valid

% <section>
Here is some text
% <title>Foo</title>
% </section>

which is probably not desirable. For another, you have to guess at the
structure if there's more than one paragraph of data, which goes against
the point of using xml in the first place.

% I'd like to indent the "There comes the text" part, but not the title.
% This is what I tried, but unfortunately it doesn't work :
%
% <xsl:template match="course/section">
% <xsl:apply-templates select="title"/>
% <ul>
% <xsl:apply-templates select="*[not(title)]"/>

Try select="node()[not(title)]", or if it's really just text,
select="text()".
 
J

Johannes Koch

Patrick said:
% I'd like to indent the "There comes the text" part, but not the title.
% This is what I tried, but unfortunately it doesn't work :
%
% <xsl:template match="course/section">
% <xsl:apply-templates select="title"/>
% <ul>
% <xsl:apply-templates select="*[not(title)]"/>

Try select="node()[not(title)]",

This selects all child nodes of section that have no _child node(s)_
called title. Try "node()[not(self::title)]" instead.
 
C

Cyrille \cns\ Szymanski

Patrick said:
% I'd like to indent the "There comes the text" part, but not the
title. % This is what I tried, but unfortunately it doesn't work :
%
% <xsl:template match="course/section">
% <xsl:apply-templates select="title"/>
% <ul>
% <xsl:apply-templates select="*[not(title)]"/>

Try select="node()[not(title)]",

This selects all child nodes of section that have no _child node(s)_
called title. Try "node()[not(self::title)]" instead.

Thanks. In fact I had added the self:: part since I read a message that
pointed out this issue.
 
C

Cyrille \cns\ Szymanski

From a design perspective, I think you're better off if you make
section have element content (that is, you wrap all your text in
additional elements). For one thing, if that's valid, then this must
also be valid

You're absolutely right, unfortunately, I am not free to do so since I'm
using existing XML documents that are already written that way.

Thanks for help,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top