Getting a substring of an attribute in XPATH

Getting a substring of an attribute in XPATH

xpath substring-after
xpath substring-before
xpath ends-with
substring in xpath selenium
xpath contains
xpath functions
xpath string-length
xpath concat example

Given

<foo>
  <bar baz="Hello, World!">
</foo>

How do I all but the last 4 characters of @baz? One of my attempts was:

/foo/bar/@baz[substring( ., 0, -4 )]

Use:

substring(/foo/bar/@baz, string-length(/foo/bar/@baz)-3) 

Do note the 3 in the expression.

The following is wrong:

substring(/foo/bar/@baz, string-length(/foo/bar/@baz)-4) 

because this returns the last 5 characters of the string value of the baz attribute.

xpath Find nodes by substring matching the end of an attribute's value, xpath Find nodes by substring matching the end of an attribute's value. Example#​. XML <Galaxy> <name>Milky Way</name>  XPath query examples and code syntax of how to use selectSingleNode to query by node text value or attribute value including substrings. Learn how to use and/or conditional arguments in xpath queries. Use selectNodes to get a node list in an XML document or XPathNavigator looking for specific attribute values.


try this: substring-before(/foo/bar/@baz,"rld!")

XPath Query Node, Attribute, and Substring, Use selectNodes to get a node list in an XML document or XPathNavigator looking for specific attribute values. The object model shown here is for the COM xml  You need to use a valid xPath function to get what you want done. Try using the tokenize() function in xPath 2.0 that should allow you to split the string at ":".


That's actually not so bad, but IIRC substring doesn't like negative indices. I tried

substring(/foo/bar/@baz, string-length(/foo/bar/@baz)-4)

Which gave me the expected result.

Using XPath Functions, This chapter describes how to use XPath functions when modeling orders in Oracle Retrieves the substring of the first argument that occurs before the first ancestor:: ancestor-or-self:: attribute:: child:: descendant:: descendant-or-self::  Hello, I meet this error: [System.Xml.XPath.XPathException] = {"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."} How could I get around it? – Ricky Nov 23 '09 at 9:53


xpath, xpath documentation: Find nodes that have a specific attribute. containing specific attributes · Finding elements containing specific text · Get nodes relative to the current node @attribute_name, It selects the attribute value for a node, if present Find nodes by substring matching of an attribute's value (case-​insensitive)#. Find nodes with a specific attribute; Find nodes by substring matching of an attribute's value; Find nodes by substring matching of an attribute's value (case-insensitive) Find nodes by substring matching the end of an attribute's value; Find nodes by substring matching the start of an attribute's value; Find nodes with a specific attribute value


XPath Examples, Ri Request Edit Rules. XPath attributes are also used to refer to parameters of Ri requests. To match an attribute in XPath, use the @ character before the name of​  The substring may have a dynamic amount of whitespace before and after it before I can get to the value, so it would be helpful to have some sort of a indexOf function to use in the XPath. I'm trying to use substring-after, but there is a good chance the XPath is 1.0, which according to this post might make this more difficult to accomplish.


4. XPath Functions and Numeric Operators, Before getting into the details of these features' uses, let's take a look at a Returns the element node with an ID-type attribute equal to the value of the passed Like most programming languages, XPath provides a substring( ) function for  Select the lang attribute of the title element that is a child of the first book child of the top element of the XML document. To get just the string value of this attribute use the standard XPath function string (): string (/*/book /title/@lang)