What does `descendant` mean in XPATHNavigator's Compile function? - c++

I've googled for few pages but failed to find the meaning of descendant word as typed out in the code below.
System::Xml::XPath::XPathExpression^ expr = nav->Compile("descendant::delivery[stockcode='ZMY201HR6US-A']");
Can someone tell me what descendant means?Thanks to anyone's helps.

descendant means:
Selects all descendants (children, grandchildren, etc.) of the current node
It comes from the XPath "axes" syntax.
http://www.w3schools.com/xpath/xpath_axes.asp
In your example, the qualifiers following it mean that the statement itself will select any delivery nodes with stockcode of ZMY201HR6US-A that are sub-nodes from the current node.

Related

Why is this xpath giving a "silly node type test" warning in IntelliJ?

The following xpath expression for XSLT 2.0:
$node/child::node()[empty(skip)]
...is intended to match any node() in the $node variable; where it is an element node with a skip child element, that node is ignored.
IntelliJ 2017.2.6 (and previous versions) does not like this expression and gives the following error:
Silly Node type test on axis 'unknown'
What is IntelliJ trying to tell me? It's the first time an IDE has called my code "silly"! Normally IDEs are much more polite... And how can an xpath axis ever be unknown, and if it really is unknown, why does my stylesheet work?

Need to extract the sibling child value using regex

In the below XML snippet,i need to extract and store in a variable the value of NAME1, when the Parent node is E1 and the element PARVW equals AG.
For the above snippet, the answer shud be: soldtoid =W
Thanks in advance for the help.
Use an existing XML parser (such as XML::LibXML) rather than writing your own shitty one!
You can access the desired node using the following XPath:
//E1EDKA2[PARVW/text()="AG"]/NAME1
I think that can be simplified to
//E1EDKA2[PARVW="AG"]/NAME1

find xpath element using a variable

im looking to find an element in a schema based on the value of a variable (that changes each time i iterate). the catch is the element could be anywhere inside the schema.
for instance:
<...
<foo>
<bar>
<bar1>BB</bar1>
<bar2>CC</bar2>
</bar>
<rab>
<rab1>DD</rab1>
</rab>
</foo>
/...>
$attribute = bar1
(then the next iteration, $attribute may equal rab1)
how would i write an expression that could find me: .../foo/bar/$attribute
the closest thing i can find is ...//*[name()=$attribute] but it doesn't work. is there any other way?
Thanks for your help!
Although the question leaves out a lot of details that may be important, you could try changing name() to local-name():
...//*[local-name()='bar1']
and see if that fixes the problem. The return value of name() includes any prefix the element name has, which could cause it not to match the value of $attribute. (#Kirill was hinting at this.)
If that doesn't solve the problem, provide more context: What is the full XPath expression? How is it being used in XSLT? How do you know it "doesn't work"? (Give expected results and actual results.)

Difference between content node and document root

I have recently read about XSLT and Xpath. But i came across document root and content node, so many times. Are these are same? Or different?
Correct me if am wrong.
The root node is the node that is the ancestor of all other nodes in the XML document. It is not an element. It has no markup: you cannot see it in a serialized document. It is the parent of the outermost element (and of any siblings that element may have, such as comments or processing instructions). The root node is matched by the XPath expression /.
Confusingly, the outermost element is sometimes called the "root element," and is referred to that way in the XML specification; but in the XSLT specification it is only called the document element. This element is matched by the XPath expression /*.
The context node (if that's what you wanted -- I'm looking at your comment about a confusion) is whatever node the processor considers to be the current node to focus on, when evaluating a particular (piece of an) XPath expression or XSLT instruction, as it occurs in the context of an XSLT stylesheet or other execution environment. See current-node in the XSLT spec. The context node is the meaning of the XPath expression ..

XSLT Get the first occurrence of a specific tag

Let's say i have a full html-document as XML-input.
How would the XSLT-file look if i only want to output the first (or any) image from the html?
One XPath expression that selects the first <img> element in a document is:
(//img)[1]
Do note that a frequent mistake -- as made by #Oded in his answer is to suggest the following XPath expression -- in general it may select more than one element:
//img[1] (: WRONG !!! :)
This selects all <img> elements in the document, each one of which is the first <img> child of its parent.
Here is the exact explanation of this frequent mistake -- in the W3C XPath 1.0 Recommendation:
NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.
A further problem exists if the document has defined a default namespace, which must be the case with XHTML. XPath treats any unprefixed name as belonging to no namespace and the expression (//img)[1] selects no node, because there is no element in the document that belongs to no namespace and has name img.
In this case there are two ways to specify the wanted XPath expression:
(//x:img)[1] -- where the prefix x is associated (by the hosting language) with the specific default namespcae (in this case this is the XHTML namespace).
(//*[name()='img'])[1]
The XPath expression the will retrieve the first image from an HTML page: (//img)[1].
See the answer from #Dimitre Novatchev for more information on problems with it.