473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

selecting a node based on partial names

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.
Jul 20 '05 #1
3 2990
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 xmlns:xsl="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
Jul 20 '05 #2
In article <76**************************@posting.google.com >,
Amit <am*******@yahoo.com> 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
pt**@interlog.com
Jul 20 '05 #3
pt**@interlog.com (Patrick TJ McPhee) wrote in message news:<2o************@uni-berlin.de>...
<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
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: thomas | last post by:
Hello all Im having problems with selecting subelements from a dynamic created RTF converted to a node-set: i have hardcodet this example: the xml structure: <xsl:variable name="test">
1
by: ADAM BETCHER | last post by:
Hi: I am using TreeView in a Microsoft Access 2000 form. I can create the tree, populate it and select items. Now I want to Programmatically select and highlight a node on the tree based...
2
by: Saurabh Sharma | last post by:
Hi, I am using Dom Parsing in Xml. I am the parent node and it has many children and each children has many children. I want to select children with a given name . Is there any method by which we...
2
by: Tymbow | last post by:
I'm building a web application that is analogous to the Windows XP file explorer in function. The left column contains a TreeView, and the right column a DataGrid populated by selecting TreeView...
1
by: jdhcards | last post by:
Hello, I've been banging my head against a problem all day without a solution, and I'm hoping you all can help. I've got a piece of XML that defines a set of elements in a flat list. Each of...
2
by: IcedDante | last post by:
Working with a sorted group, the inability to use following-sibling (which uses Document Order) and convert an RTF (not avaible with the Parser that we are using) hampered our ability to solve the...
0
by: Satiz | last post by:
Hi All, I've a TreeView(IE Webcontrol) and a DataGrid in my VS.Net 2003 ASP.Net web form. My problem : If i select a particular node(it may be parent, child or leaf), then the corresponding...
1
by: tbucha3 | last post by:
I have two tables that I have to get information from to produce another table. Both tables come from separate resources, one has partial accounts with addresses and the other has complete account...
1
by: DeveloperX | last post by:
Hi, I should probably start with my XML as it makes it easier to explain the problem. <?xml version="1.0" encoding="utf-8" ?> <aa note="top level"> <b1 note="b1"> <c1 note="b1 c1"> <d1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.