Solved

XPath: How to find node contaning substring of value?

Posted on 2003-12-02
6
435 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 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