Solved

XPath: How to find node contaning substring of value?

Posted on 2003-12-02
6
441 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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