I am writing some scripts to change some values in config (XML) files. The script will take XPath expressions and replacement values to be replaced in a source document.
If the node is found in the source document, then the value will be replaced, but if the node is not found, I need to create a new element and add required elements with attributes.
For example, in a web.config if appSetting exists, then change its value, if not then create a new one
/configuration/appSettings/add[#key='ClientValidationEnabled']/#value
I'm wondering if it's possible to read the XPath as an expression that lets me walk it and create a new element if needed.
Related
I'm parsing an XML file with Nokogiri.
Currently, I'm using the following to get the value I need (the document includes multiple Phase nodes):
xml.xpath("//Phase[#text=' = STER P=P(T) ']")
But now, the uploaded XML file can have a text attribute with a different value. Thus, I'm trying to update my code using a regular expression since the value always contains STER.
After looking at a few questions on SO, I tried
xml.xpath("//Phase[#text~=/STER/]")
However, when I run it, I get
ERROR: Invalid predicate: //Phase[#text~=/STER/] (Nokogiri::XML::XPath::SyntaxError)
What am I missing here?
Alternatively, is there an XPATH function similar to starts-with` that looks for the substring within the entire value and not just at the beginning of it?
There are two problems with your code: first off, there is no =~ operator in XPath. The way to test whether text matches a regex is using the matches function:
//Phase[matches(#text, 'STER')]
Secondly, regex matching is a feature of XPath 2.0, but Nokogiri implements XPath 1.0.
Luckily, you are not actually using any regex features, you are simply checking for a fixed string, which can be done with XPath 1.0 using the contains function:
//Phase[contains(#text, 'STER')]
Problem Statement: in my API response, I am getting same set of xml tag repeated multiple times but with different values of the underlying tags which are also repeating across xml response. Also those two underlying values have mapping to each other. I need to extract all the mappings from response and write them to SQL. I am having trouble with extracting the mapping values from the API response.
I am trying to use regular expression extractor to fetch that repetitive main tag which hold those two values. Then I am trying to use for each loop on the output variable of regular expression extractor. And will then write respective values to target table for each of the iteration.
Following tag is repeating multiple times for each cycle. I need to fetch the two values present under <Value> tag for each of the <Object> tag. For example '{abc-def}' and 'D12345' in this case for this particular instance....and so on.
<Object classId="QueryResultRow"><Property i:type="fn40:SingletonId" propertyId="Id"><Value>{abc-def}</Value></Property><Property i:type="fn40:SingletonString" propertyId="DCN"><Value>D12345</Value></Property></Object>
I am unable to get the required two values from each of the tag while retaining the mapping. Also, I am not sure how to use only one of the generated variable from the output of regular expression as it's creating 4 types of variables for each target xml tag.
objVal=<Object classId="QueryResultRow"><Property i:type="fn40:SingletonId" propertyId="Id"><Value>{abc-def}</Value></Property><Property i:type="fn40:SingletonString" propertyId="DCN"><Value>D112345</Value></Property></Object>
objVal_g=1
objVal_g0=<Object classId="QueryResultRow"><Property i:type="fn40:SingletonId" propertyId="Id"><Value>{abc-def}</Value></Property><Property i:type="fn40:SingletonString" propertyId="DCN"><Value>D12345</Value></Property></Object>
objVal_g1=<Property i:type="fn40:SingletonId" propertyId="Id"><Value>{abc-def}</Value></Property><Property i:type="fn40:SingletonString" propertyId="DCN"><Value>D12345</Value></Property>
I would need to use only objVal from here and I am trying to use
Regular expression extractor
Flow of my Test
For Each loop to extract object tags
second for loop to extract two values out of extracted object tags
use of variable created in 4th step in my jdbc sampler
After you have xml in objVal variable,
Use XPath Extractor using JMeter variable Name to use your objVal variable
Use XPath query as /Object/Property/Value
And if Match No. is -1 you will get all values
objVal_value={abc-def}
aa_value_1={abc-def}
aa_value_2=D112345
allows the user to extract value(s) from structured response - XML or (X)HTML - using XPath query language
You can use the below regular expression to fetch the required values.
propertyId="Id"><Value>(.*?)</Value>(.*)propertyId="DCN"><Value>(.*?)</Value>
The values will be present in the below variables.
objval_1_g0
objval_1_g2
objval_2_g0
objval_2_g2
objval_3_g0
objval_3_g2
objval_4_g0
objval_4_g2
You can use a debug sampler to check the variables value.
For purposes to automatically replace keywords with links based on a list of keyword-link pairs I need to get text that is not already linked, not a script or manually excluded, inside paragraphs (p) and list items (li) –- to be used in Drupal's Alinks module.
I modified the existing xpath selector as follows and would like to get feedback on it, if it is efficient or might be improved:
//*[p or li]//text()[not(ancestor::a) and not(ancestor::script) and not(ancestor::*[#data-alink-ignore])]
The xpath is meant to work with any html5 content, also with self closing tags (not well-formed xml) -- that's the way the module was designed, and it works quite well.
In order to select text node descendant of p or li elements that are not descendant of a or script elements, you can use this XPath 1.0:
//*[self::p|self::li]
//text()[
not(ancestor::a|ancestor::script|ancestor::*[#data-alink-ignore])
]
Your XPath expression is invalid. You are missing a / before text(). So a valid expression would be
//*[p or li]/text()[not(ancestor::a) and not(ancestor::script) and not(ancestor::*[#data-alink-ignore])]
But without an XML source file it is impossible to tell if this expression would match your desired node.
I have an XML file to parse in which the element tags are of the form:
<mensa-1>
..
</mensa-1>
<mensa-2>
..
</mensa-2>
Is it possible to parse such elements via Xpath when the element names differ via a number at the end?
The following XPath expression returns all the elements whose names start with "mensa-":
//*[starts-with(name(),'mensa-')]
I have such content of html file:
<a class="bf" title="Link to book" href="/book/229920/">book name</a>
Help me to construct xpath expression to get link text (book name).
I try to use /a, but expression evaluates without results.
If the context is the entire document you should probably use // instead of /. Also you may (not sure about that) need to get down one more level to retrieve the text.
I think it should look like this
//a/text()
EDIT: As Tomalak pointed out it's text() not text
Have you tried
//a
?
More specific is better:
//a[#class='bf' and starts-with(#href, '/book/')]
Note that this selects the <a> element. In your host environment it's easy to extract the text value of that node via standard DOM methods (like the .textContent property).
To select the actual text node, see the other answers in this thread.
It depends also on the rest of your document. If you use // in the beginning all the matching nodes will be returned, which might be too many results in case you have other links in your document.
Apart from that a possible xpath expression is //a/text().
The /a you tried only returns the a-tag itself, if it is the root element. To get the link text you need to append the /text() part.