Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XPath: How to find node contaning substring of value?

Posted on 2003-12-02
6
Medium Priority
?
464 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 total points
ID: 9864354
ok, if you haven't more questions, please close the question.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

885 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