Solved

XPath: How to find node contaning substring of value?

Posted on 2003-12-02
6
417 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
Comment Utility
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
Comment Utility
Thanks  dualsoul, but I forgot to mention that I need XPath, not XSLT
0
 
LVL 15

Expert Comment

by:dualsoul
Comment Utility
> 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 26

Expert Comment

by:rdcpro
Comment Utility
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
Comment Utility
Thanks guys
0
 
LVL 15

Accepted Solution

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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, …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now