Solved

XPath: How to find node contaning substring of value?

Posted on 2003-12-02
6
455 Views
Last Modified: 2006-11-17
Hi!
I am new to XML and I would appreciate any help. I need to find all XML nodes which have value which includes given substring. For example:
<name> abcd </name>
<name> cdfg </name>
<name> cbcs </name>
I will need to get list of nodes contaning substring "bc" (first and third nodes). I know how to search xml to match entire value string. But what should I do to match substring of value string?
0
Comment
Question by:gelbert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:dualsoul
ID: 9859858
i' assuming you have xml like this, for instance:

<?xml version="1.0" encoding="UTF-8"?>
<root>
      <name> abcd </name>
      <name> cdfg </name>
      <name> cbcs </name>
</root>

so, xslt to get  what you want:
....................................
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<output>
    <xsl:for-each select="/root/name[contains(.,'bc')]">
                <xsl:copy-of select="." />
    </xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
.....................................

will produce:
<output>
<name> abcd </name>
<name> cbcs </name>
</output>
, or if you don't use xslt, just use following XPath:

/root/name[contains(.,'bc')]

to get nodes with substring 'bc'
0
 
LVL 8

Author Comment

by:gelbert
ID: 9860351
Thanks  dualsoul, but I forgot to mention that I need XPath, not XSLT
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 9861287
> Thanks  dualsoul, but I forgot to mention that I need XPath, not XSLT
you read inattentive :)
i wrote:
  if you don't use xslt, just use following XPath:  /root/name[contains(.,'bc')]  
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 26

Expert Comment

by:rdcpro
ID: 9861768
But, if you're using MSXML, first make sure you tell the DOM document object that you're using XPath (it defaults to XSL Patterns):

var xmlDoc = new ActiveXObject("Msxml2.DomDocument");
xmlDoc.setProperty("SelectionLanguage", "XPath");
xmlDoc.load("path/to/xml.xml")

var oNodeList = xmlDoc.selectNodes("/root/name[contains(.,'bc')]")

Regards,
Mike Sharp
0
 
LVL 8

Author Comment

by:gelbert
ID: 9863482
Thanks guys
0
 
LVL 15

Accepted Solution

by:
dualsoul earned 125 total points
ID: 9864354
ok, if you haven't more questions, please close the question.
0

Featured Post

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question