Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > selecting a node based on partial names

Reply
Thread Tools

selecting a node based on partial names

 
 
Amit
Guest
Posts: n/a
 
      08-11-2004
I have a quick question. Given test XML:

<root>
<ns1:sub>1</ns1:sub>
<ns2:sub>2</ns2:sub>
<ns3:sub>3</ns3:sub>
</root>

is there a way to select all nodes that contain the word "sub". (Due
to namespace constraints, I will be getting the subnodes as <ns1:sub>,
<ns2:sub> and so on.)

regards,
Amit.
 
Reply With Quote
 
 
 
 
Scott Stearns
Guest
Posts: n/a
 
      08-12-2004
Amit wrote:
> I have a quick question. Given test XML:
>
> <root>
> <ns1:sub>1</ns1:sub>
> <ns2:sub>2</ns2:sub>
> <ns3:sub>3</ns3:sub>
> </root>
>
> is there a way to select all nodes that contain the word "sub". (Due
> to namespace constraints, I will be getting the subnodes as <ns1:sub>,
> <ns2:sub> and so on.)
>
> regards,
> Amit.


You can use the XPath function 'contains' to search for 'sub' within the
element names.

For this XML:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sub.xsl"?>
<root>
<ns1:sub>1</ns1:sub>
<ns2:sub>2</ns2:sub>
<ns3:sub>3</ns3:sub>
<ns1:nomatch>1</ns1:nomatch>
<ns2:nomatch>2</ns2:nomatch>
<ns3:nomatch>3</ns3:nomatch>
</root>

The following XSL transformation selects those 'sub' elements:

<?xml version="1.0"?>
<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/root">
<html><head/>
<body>
<xsl:for-each select="*">
<xsl:if test="contains(name(.),'sub')">
<h2><xsl:value-of select="name(.)"/></h2>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Hopefully that is what you were looking for. Have fun!
-Scott
 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      08-15-2004
In article < >,
Amit <> wrote:
% I have a quick question. Given test XML:
%
% <root>
% <ns1:sub>1</ns1:sub>
% <ns2:sub>2</ns2:sub>
% <ns3:sub>3</ns3:sub>
% </root>
%
% is there a way to select all nodes that contain the word "sub". (Due
% to namespace constraints, I will be getting the subnodes as <ns1:sub>,
% <ns2:sub> and so on.)

Assuming you use xpath to do the selection, the function local-name()
returns the name of the element sans name-space prefix. In XSLT, you
could have

<xsl:template match='local-name(.) = "sub"'>
...
</xsl>

Question: if this is really what you want, why do you have name-spaces
there at all?
--

Patrick TJ McPhee
East York Canada

 
Reply With Quote
 
Robin Johnson
Guest
Posts: n/a
 
      08-16-2004
(Patrick TJ McPhee) wrote in message news:<>...
> <xsl:template match='local-name(.) = "sub"'>


Surely you mean <xsl:template match="*[local-name() = 'sub']">

> Question: if this is really what you want, why do you have name-spaces
> there at all?


Maybe ns1:sub and ns2:sub are to be treated differently at some other point.
--
Robin Johnson
Lead Developer, enCircle Solutions Ltd.
first initial last name at encircle dot co dot uk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help selecting multiple checkboxes based on partial value JL Javascript 7 03-10-2008 01:10 AM
I want to select all the node names beneath a given node thecolour XML 2 06-27-2007 10:46 AM
I want to select all the node names beneath a given node thecolour XML 0 06-26-2007 04:01 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM



Advertisments