Need to extract the sibling child value using regex - 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

Related

how to change links in a xml or any other file with regex or some other way?

I have a file with a lot of xml nodes and they are linked together with an id. I need to change the id of a node as well as the link.
<event id="12345">
<action>6789</action>
</event>
<action id="6789">
<name>pre-filter1</name>
<someotherlink>45678</someotherlink>
</action>
I need to change the id of action nodes and the reference wherever it is being linked from. I was looking into regex because I have to do it for some action nodes only with some specific name like pre-filter here. the id needs to be processed by some logic before replacing with the new value. the order of nodes is random.
I only need to do it once for the whole file and any way is fine. also time complexity is not a constraint.
Any help is appreciated.
Perl supports using functions on the replacement of a regular expression. Not sure about other languages.
If you are not using perl, you may do the following:
1) Get all action ids for a given name with this regexp:
<action\s*id="(\d+)">(?=[^=]*<name>pre-filter\d<\/name>).*?<\/action>
https://regex101.com/r/Q7lKgx/1
2) Convert values and store both original id and converted value in a hash.
3) Loop the hash and use a regexp to replace the id with the new value
This matches both action and action id:
(<action(?:\s*id="|>))(THE_ID)("|<\/action)> ==> replace with \1NEW_ID\3
Anyways, parsing XML with regexes is usually not a good idea, so It would be even better to use some library to parse xmls.

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

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.

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.)

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.

Nesting of xsl:key() within xsl:key() & combining results of xsl:key()

I have 2 unrelated questions. I need to do a grouping of data using XSLT. I need this to function like how a nested IF within an IF would function. After which, I need to group the data so that I can split it into multiple files according to the Group condition.
Using XSLT Version 1.0 :
Q1) How do you nest a key() within another key() (i.e. Use the result nodes returned from the first key() as current node-list to search for 2nd key() condition to group my data)?
Q2) Can I combine the results of 2 key() functions? Say, I want to execute code for all nodes with Key values of "A" and "B".
Does anyone know how to solve Q1 and Q2?
Appreciate your help very much! I hope the questions are clear enough. Let me know if you need examples of input and output.
Regards,
Melita.
On Q2, did you mean "values of 'A' OR 'B'"? The key for an element has only one value.
<xsl:for-each select="key('myKey', 'A') | key('myKey', 'B')">
...
</xsl:for-each>