Way to determine XPath to retrieve data of a specific attribute - javascript

I am working on an integration and the response I am getting in XML format. I need to parse it and get the attribute values using javascript. I am trying to get value from node <ab:specific_field> with <ab:field_reference>id is commodity or cosmetic. I am struggling to go trough nodes and extract <ab:Value> node value which is the value I need to extract.
I tried with //ab:Main_Data/ab:specific_field[1]/ab:Value, but no luck. Can anyone help me to write a correct XPath to extract value from <ab:value> node.
<ab:Response_Data>
<ab:MainData>
<ab:reference>....</ab:refernce>
<ab:information....<ab..info>
<ab:specific_field>
<ab:Field_Reference>
<ab:ID type="WID">123</ab:ID>
<ab:ID wd:parent_id="custom-API-Service" ab:parent_type="Integration_Document_Name" ab:type="Integration_Document_Field_Name">Commodity</ab:ID>
</ab:Field_Reference>
<ab:Value>Medicine</ab:Value>
</ab:specific_field>
<ab:specific_field>
<ab:Field_Reference>
<ab:type="WID">1234</ab:ID>
<ab:ID wd:parent_id="custom-API-Service" ab:parent_type="Integration_Document_Name" ab:type="Integration_Document_Field_Name">Cosmetic</ab:ID>
</ab:Field_Reference>
<ab:Value>Powder</ab:Value>
</ab:specific_field>
</ab:MainData>
</ab:Response_Data>

Your XML has errors. If you have more than one ab:ID fields you can filter one of them with a property.
To get ab:ID nodes:
//ab:ID[#wd:parent_id="custom-API-Service"]
To get ab:ID text nodes:
//ab:ID[#wd:parent_id="custom-API-Service"]/text()

Try this
//ab:maindata/ab:specific_field[.//ab:id[normalize-space()='commodity' or normalize-space()='cosmetic']]/ab:value

Related

Extracting values from JMSE

I want extract text values from every sub objects the given JSON structure . For do that I have used following JMSEPath query translations.en.*[0].[*].children.text but I was not unable to extract the value . Can someone suggest me a correct query or other approches
It may be better if you add to the question the expected output as well.
Making a big assumption that you want all text values "flatten up" into a single array you can try this: translation.en.*[].children[].text
Ref https://jmespath.org/tutorial.html#flatten-projections

How to retrieve the value of the first field in an object without its name?

I am using the REST Countries API to retrieve data from different countries, the point is to generate a HTML code to insert, depending on the country data.
But as I'm trying to point the language data in my HTML code, it of course returns me an object.
Here are two examples of the data from the API:
languages: {cat: 'Catalan'}
languages: {fra: 'French'}
The line of code in my HTML:
<p class="country__row"><span>🗣️</span>${data.languages}</p>
I would like to retrieve the values of the first field in those objects (Catalan or French) without having to, of course, write ${data.languages.fra} in my HTML code.
Thank you for your help.
I tried ${data.languages[0]} and ${data.languages[0].value}, doesn't work.
You can use the Object.keys() function to get the list of keys from an object.
There are other similar functions as well that you can use such as Object.values() or Object.entries().
As far as your use case is concerned try any one of the solutions mentioned below:
${data.languages[Object.keys(data.languages)[0]]}
// The `0` index can be replaced with i if using a loop.
${Object.values(data.languages)[0]}
You can write as Object.values(data.languages) which would give you values of object.

indexOf or search function issue for XML tag to get the value

I have a big XML with unique XML tag which is coming as string
I want to extract the value of an XML element
I tried the following
a. xmlString.substring(this.responseText.search('<emp:keyAddress>')+5, this.responseText.search('</emp:keyAddress>'))
b. xmlString.substring(this.responseText.indexOf('<emp:keyAddress>')+5, this.responseText.indexOf('</emp:keyAddress>'))
I am getting the following issues
a. Referenced to undeclared namespace prefix: 'emp'
b. Problem creating XSL transformer for string
How to get the value of an XML tag using Javascript? I don't like neither searchText or indexOf where i need to hard-code the length as it may differs in the future
Please advise.
Thanks

How to save HTML element ID and its hierarchy in Database column?

I want to save an element's ID in a database column.
I also need to save the complete DOM hierarchy of that element in the same column. Later i will use this column information to get the value of that element using JavaScript parsing and traversing.
I am uncertain about which pattern to use to save this information.
e.g:
I am thinking about following patterns:
elementLocation = "iframe[iframeName],iframe[iframeName2],element[elementID]"
elementLocation= "frame[frameName],frame[frameName2],element[elementID]"
elementLocation= "i[iframeName],i[iframeName2],e[elementID]"
elementLocation= "f[iframeName],f[iframeName2],e[elementID]"
Please suggest a better pattern that can be used to represent any kind of hierarchy.
you can use the XML Path language, which is designed for this purpose.

How to get multiple "listKey" values from a Struts 2 <s:select>?

I have a code like this:
<s:select id="s" list="list" listKey="id" listValue="displayValue">
When I get the value from the object I'll always get the id.
How can I change the "listKey" value dinamically so I could get for example, the name of the object (supposing other of my attributes besides id is name ) instead always de id?
I was trying some code in jQuery like this:
function changeListKey(){
var id = $("#s").val();
$('#s').attr('listKey','name');
var name = $("#s").val();
document.write(name); // Only to test if I could get the value
}
When I execute it it doesn't seem to change the "lisKey" value so I could get another one, what would be the solution?
Thanks a lot in advance.
John Smith.
Why would you want to mix up what's used as the key value? That would make a mess on the server side.
In any case, listKey isn't an HTML attribute, it's a custom tag attribute, and is meaningless after the HTML has been rendered and sent to the client. You would need to iterate over the HTML select element's options collection and change the values on the options.
All this said, you'd be much better off doing any bizarre manipulations like this in the action itself, and exposing only the desired key/value pairs to the JSP, either in a list, or more easily, in a map. Using a map eliminates the need to provide listKey/listValue attributes--just use the map's key as the option text, and the value as the option's value.

Categories

Resources