XPath Problem: Select all childnodes, that names are neither "name1" nor "name2"

A

adurth

Hi!
I wanna copy all childnodes of the current nodes except those with
basenames "name1" or "name2".
Something like
<xsl:copy-of select=" basename not equal ('name1' or 'name2') "/>

Can someone help please?
Thanks,
Andreas
 
P

p.lepin

I wanna copy all childnodes of the current nodes except
those with basenames "name1" or "name2".

What do you mean by 'basename'? Element name, attribute,
child element? Please stick to standard terms if you want
others to understand you.
<xsl:copy-of
select=" basename not equal ('name1' or 'name2') "/>

You don't have any previous coding experience, do you?

Depending on what precisely you intend to do, the XPath
expression might look like:

*[not(self::name1) and not(self::name2)]

*[local-name()!='name1' and local-name()!='name2']

*[@basename!='name1' and @basename!='name2']

*[basename!='name1' and basename!='name2']

Note that it is elementary good design to implement
template-based copying instead of copy-of (untested):

<xsl:template match="foo">
<xsl:apply-templates mode="selective-copy"/>
</xsl:template>
<xsl:template match="name1|name2" mode="selective-copy"/>
<xsl:template
match=
"
*[not(self::name1) and not(self::name2)]
" mode="selective-copy">
<xsl:copy-of select="."/>
</xsl:template>

This way you can easily reuse selective-copy whenever
needed, and you would only need to change anything in one
place in case you needed alterations to the algorithm.
 
A

adurth

Hi!
'baseName' is a standard term in VB, it returns the element name.
Does it matter if I have coding experience?
Yes, template is much better. But it wouldn´t have fit in one column,
would it? Everyone has understood me without reading several lines of
unformatted, unhighlighted code.
Thank you very much, your suggestion works fine.

Greetings,
Andreas
 
P

p.lepin

Please quote what you're replying to.

'baseName' is a standard term in VB, it returns the
element name.

This is not a VB newsgroup, however. When asking
VB-specific questions in a VB newsgroup, by all means, go
ahead and use the terms accepted in the VB
community--that's the right thing to do anyway. On the
other hand, when asking questions in an XML newsgroup,
using VB-specific terms is quite counter-productive.
Does it matter if I have coding experience?

That's something you'll have to ask your employer, not me.
I just made an observation regarding the fact that:

basename not equal ('name1' or 'name2')

....is a somewhat surprising way to express what J. Random
Codegrinder would probably express as either:

basename not equal 'name1' and basename not equal 'name2'

....or:

basename not in ( 'name1' , 'name2' )
Yes, template is much better. But it wouldn´t have fit
in one column, would it?

It was just a random observation. Well, not entirely
random, there was my doubtlessly nefarious intention to
give you a pointer on good practices in case you haven't
realised that yourself yet.
Everyone has understood me without reading several lines
of unformatted, unhighlighted code.

In this case you're absolutely right. Note however, that
for harder questions it's a good practice to post what is
commonly called 'minimum complete example'; and in that
case you should take care of the formatting while my gvim
will take care of highlighting.
 
R

Richard Tobin

That's something you'll have to ask your employer, not me.
I just made an observation regarding the fact that:

basename not equal ('name1' or 'name2')

...is a somewhat surprising way to express [...]

It's not *that* implausible. For example, in Python you can say:

basename in ('name1', 'name2')

and in XPath (!) you can test whether the foo attribute is equal to
either the bar or baz attribute by saying:

@foo = (@bar | @baz)

which is really very similar. (There is a trap for the unwary in
using != in this case.)

-- Richard
 
P

p.lepin

It's not *that* implausible. For example, in Python you
can say:

basename in ('name1', 'name2')

Well, I'm obviously aware of facilities like that in many
of the modern programming languages since I explicitly
mentioned it in the part of my previous post you snipped.
and in XPath (!) you can test whether the foo attribute
is equal to either the bar or baz attribute by saying:

@foo = (@bar | @baz)

Mind-boggling. Note however, that unless I'm much mistaken,
| is very different from or, and not in the way | is
different from || in C either. = is not precisely 'equal'
when working with nodesets, too.
 
A

adurth

Well, I'm obviously aware of facilities like that in many
of the modern programming languages since I explicitly
mentioned it in the part of my previous post you snipped.

Yes, you are obviously a code-guru and I bow in awe of such
wisdom ;)).
Never mind I just find the emotions often coming along with such
unimportant matters quite funny.

Greets,
Andreas
 
R

Richard Tobin

It's not *that* implausible. For example, in Python you
can say:

basename in ('name1', 'name2')
[/QUOTE]
Well, I'm obviously aware of facilities like that in many
of the modern programming languages since I explicitly
mentioned it in the part of my previous post you snipped.

Oh yes, so you did.

-- Richard
 

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
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top