XSLT Select nodes without text-node children whose names starts with specifix text

M

Michael Reiche

Question on XSL expression

Got this XML:

<Body>
<Page>
<Line no="9" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
<Line no="10" detail="true">
<onefield>onefieldstext</onefield>
<fgman9>fgmanfieldstext</fgman9>
<twofield>twofieldstext</twofield>
</Line>
<Line no="11" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
<Line no="12" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
<Line no="13" detail="true">
<onefield>onefieldstext</onefield>
<fgman5>fgmanfieldstext</fgman5>
<twofield>twofieldstext</twofield>
</Line>
<Line no="14" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
</Page>
</Body>

I would select the <Line/> nodes without text-node children whose names
is starting with "fgman" - in this example it is all <Line/> _except_
<fgman9/> and <fgman5/> in <Line/> with @no of 10 and 13.

I know that this works:

<xsl:for-each select="Body/Page/Line[@detail]">

<xsl:if test="count(fgman9|fgman5) = 0">
L<xsl:value-of select="@no"/>:<xsl:value-of select="onefield"/>
</xsl:if>

<xsl:if test="fgman9">
<xsl:value-of select="@no"/> Ouuch</xsl:if>

<xsl:if test="fgman5">
<xsl:value-of select="@no"/> Whoops</xsl:if>

</xsl:foreach>

But as the <fgmanXXX/> nodes could be named fgman0 to fgman1000 I would
prefer not to list everyone as arguments to the count() expression.

/Reiche
 
M

Markus Spath

Michael said:
I know that this works:

<xsl:for-each select="Body/Page/Line[@detail]">

<xsl:if test="count(fgman9|fgman5) = 0">
L<xsl:value-of select="@no"/>:<xsl:value-of select="onefield"/>
</xsl:if>

<xsl:if test="fgman9">
<xsl:value-of select="@no"/> Ouuch</xsl:if>

<xsl:if test="fgman5">
<xsl:value-of select="@no"/> Whoops</xsl:if>

</xsl:foreach>

within a xsl:choose instruction you could filter out all elements having
unwanted children using starts-with(), eg:

<xsl:choose>
<xsl:when test="*[starts-with(name(),'fgman')]" />
<xsl:eek:therwise>
L<xsl:value-of select="@no"/>:<xsl:value-of select="onefield"/>
</xsl:eek:therwise>
</xsl:choose>

markus
 
D

Dimitre Novatchev [MVP]

Michael Reiche said:
Question on XSL expression

Got this XML:

<Body>
<Page>
<Line no="9" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
<Line no="10" detail="true">
<onefield>onefieldstext</onefield>
<fgman9>fgmanfieldstext</fgman9>
<twofield>twofieldstext</twofield>
</Line>
<Line no="11" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
<Line no="12" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
<Line no="13" detail="true">
<onefield>onefieldstext</onefield>
<fgman5>fgmanfieldstext</fgman5>
<twofield>twofieldstext</twofield>
</Line>
<Line no="14" detail="true">
<onefield>onefieldstext</onefield>
<twofield>twofieldstext</twofield>
</Line>
</Page>
</Body>

I would select the <Line/> nodes without text-node children whose names

A text node does not have a name -- probably you mean "whose parent's name"
..
is starting with "fgman" - in this example it is all <Line/> _except_
<fgman9/> and <fgman5/> in <Line/> with @no of 10 and 13.


Use:

/*/*/Line[not(*[starts-with(name(), 'fgman')])] [text()]


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
 
M

Michael Reiche

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

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top