How to get the values of an element by name - tsql

I am using T-SQl and xPath to query an xml doc. and I need to extract the value of an element by name
This is the way I initially implemented the code "using element indexing to access the element" but since element can have multiples element of , it does not longer work properly.
n.value(''./ROLES[1]/ROLE[1]/BORROWER[1]/GOVERNMENT_MONITORING[1]/HMDA_RACES[1][./HMDA_RACE/EXTENSION/ULDD:OTHER/ULDD:HMDA_RACE_EXTENSION/ULDD:HMDA_RACE_DETAIL/ULDD:HMDARaceType][1]'',''VARCHAR(100)'')
This is the xml I am querying
<HMDA_RACES>
<HMDA_RACE>
<EXTENSION>
<ULDD:OTHER xmlns:ULDD="http://www.datamodelextension.org/Schema/ULDD">
<ULDD:HMDA_RACE_EXTENSION>
<ULDD:HMDA_RACE_DESIGNATIONS>
<ULDD:HMDA_RACE_DESIGNATION>
<ULDD:HMDARaceDesignationType>Samoan</ULDD:HMDARaceDesignationType>
</ULDD:HMDA_RACE_DESIGNATION>
</ULDD:HMDA_RACE_DESIGNATIONS>
<ULDD:HMDA_RACE_DETAIL>
<ULDD:HMDARaceType>NativeHawaiianOrOtherPacificIslander</ULDD:HMDARaceType>
</ULDD:HMDA_RACE_DETAIL>
</ULDD:HMDA_RACE_EXTENSION>
</ULDD:OTHER>
</EXTENSION>
</HMDA_RACE>
<HMDA_RACE>
<EXTENSION>
<ULDD:OTHER xmlns:ULDD="http://www.datamodelextension.org/Schema/ULDD">
<ULDD:HMDA_RACE_EXTENSION>
<ULDD:HMDA_RACE_DETAIL>
<ULDD:HMDARaceType>White</ULDD:HMDARaceType>
</ULDD:HMDA_RACE_DETAIL>
</ULDD:HMDA_RACE_EXTENSION>
</ULDD:OTHER>
</EXTENSION>
</HMDA_RACE>
</HMDA_RACES>
this is what I am trying but I am not getting the result I want.
value(''./ROLES[1]/ROLE[1]/BORROWER[1]/GOVERNMENT_MONITORING[1]/HMDA_RACES[child = "ULDD:HMDARaceType"][1]'',''VARCHAR(100)'')
a successful query would return value "NativeHawaiianOrOtherPacificIslander White". I am getting just the first value "NativeHawaiianOrOtherPacificIslander"

Just in terms of the xpath expression, and using the xml code in your question, this expression
*//HMDARaceType/text()
or this:
//*[local-name()='HMDARaceType']/text()
selects
NativeHawaiianOrOtherPacificIslander
White
Is that what you're looking for?

Related

iText DITO - How can I use a fields Data name as a reference in a calculation?

In iText's DITO template designer product, I have one field which has a fairly complex calculation. In another field, I want to use the result of that calculation.
The tooltip says "Identifier used for referencing fields in calculation expressions". So I assumed if I named it blah I can use it in a calculation in another field like this: sum({{blah}},42) (e.g. add 42 to the value of blah). But if I do that I get the error
Unable to resolve reference: "{{blah}}" in node with tag: <span> and text:
The iText DITO documentation does not elaborate on this feature at all. I'm evaluating iText DITO and there does not seem to be any way to get support while evaluating the product.
Suppose I have a field with a very complex calculation. In order to reuse the calculation result, I can do the following:
Give a name to this field (in the image below I've chosen name "total")
Insert a calculation to another field (where I want to reuse "total")
Add "total" as the calculation's body:
That's it, now the total value is reused:
I think the documentation you are looking for is at https://kb.itextpdf.com/home/ditokb/latest/manuals/itext-dito-editor/calculations
You have to give the rich text element a name in the properties to be able to reference it on other rich text element and/or calculations.
See image example at https://kb.itextpdf.com/home/ditokb/files/latest/68620314/68620309/1/1660291029000/image2020-5-27_17-56-41.png

How do I get the nth element with a certain name in the entire XML document?

so will something like this work using Hpple Xpath
//a[4]
The fourth tag in a html tree?
Or do i need to do it programmatically by counting in a for() loop?
The XPath for the fourth <a> in an HTML document is:
(//a)[4]
Your example //a[4] will produce a set of all <a>s that are the fourth <a> in their respective parent, and that is not what you want here.
See also: https://stackoverflow.com/a/14209492/1945651

jQuery: Select all 'select' elements with certain val()

Does anyone know of an easy way, using jQuery, to select all <select> elements whose val() attribute yields a certain value?
I'm trying to do some validation logic and would like to just select all those elements with a single selector, then apply a warning class to each of their parents. This I know how to do once I select all the elements, but I didn't see a selector that handles this case.
Am I going to have to select all of the <select> elements into a selector, then iterate through them and check each of their values? I was hoping there would be a simpler way.
Thanks.
Why doesn't select[value=x] work? Well firstly because <select> doesn't actually have a value attribute. There is not a single value of a select box: there may be no selected options (there shouldn't normally be, but there can be in at least IE), and, in a <select multiple>, there can be any number of selected options.
Even input[value=x] doesn't work, even though <input> does have a value attribute. Well, it does work, it just doesn't do what you think. It fetches the value of the value="..." attribute in the HTML, not the current value you have entered into the form. The value="..." attribute actually corresponds to the defaultValue property and not value.
Similarly, option[value=x][selected] doesn't work because it is checking the <option selected> attribute from the HTML source (selected attribute -> defaultSelected property) and not the current selectedness of the option (selected property not attribute) - which might have changed since the page was loaded.
Except in IE, which gets the value, selected etc form attributes wrong.
Except (again): Tesserex's example may seem to work, and the reason for that is that that it's using a non-standard jQuery-specific selector, :has. This causes the native querySelectorAll methods of modern browsers to fail, and consequently jQuery falls back to its own (native JavaScript, slow) selector engine instead. This selector engine has a bug where it confuses properties for attributes, allowing [value=x] to do what you expected, and not fail like it should! (Update: this is probably no longer the case in newer jQuery versions.)
Summary: form field state checking and selectors don't mix. Apart from these issues, you also have to worry about escaping issues - for example, what if the value you want to test against contains quotes or square brackets?
So instead, yes, you should check it manually. For example using a filter:
$('select').filter(function() {
return $(this).val()==='the target value';
}).parent().addClass('warning');
(There is a value property in HTML5 and supported by modern browsers, that when you read it gives you the value of the first selected <option>. jQuery's val() is safe to use here because it provides the same method of getting the first selected option even on browsers that don't support this.)
The existing answers don't work on select tags, but I found something that does. Ask for a select that has a selected option.
$("select:has(option[value=blah]:selected)")
You can use :
$("select[value=X]");
where X is the value against which you want to check the select's value.
Attribute selectors Is what you're looking for I believe.
Something like $+('element[attribute="value"]')
See also:
*= anywhere
^= starts with
$= ends with
~= contains word
etc.
You can create a change event that puts the value in a custom attribute on the select element whenever the value changes. You can then use a simple selector to find all of the select elements that have that value. For example:
$("select").on("change", function (e) {
var $select = $(e.currentTarget);
$select.attr("select-value", $select.val());
});
And then you can do this:
var $matches = $("select[select-value='" + searchVal + "']");
$matches will have all of your matching selects.
This is a lot easier than having to iterate through elements. Remember to set select-value to the initial value when rendering the page so you don't need to trigger a change event for each select so the select-value is set.

XPath Powershell query for a string

I'd like to know how to search for a string within an xml document. The object type is System.Xml.XmlNode.XmlDocument. The string can be anything with the document. I.e. attribute or element.
I tried
Select-Xml -Xml $xml -XPath "./Test"
but got no results
The pattern you are trying to use selects root nodes named Test.
You could use the pattern (//text()|//#*)[contains(string(), "test")], that selects the attributes that contain the string test or the text nodes that contain it (i.e. not the elements).
But you want to select the elements, right? Using (//*|//#*)[contains(., "test")] does that, but it selects elements that contain the string test, even if it is through some child element, which is not what is wanted either.
So I guess you'll have to use something like (//*[contains(text(), "test")]|//#*[contains(., "test")]), which gives you what you want, but is not very pretty.

Why following XPath statement returns all "a" elements?

print $tree->findvalue('//a[1]');
I am using HTML::TreeBuilder::XPath in perl. Now i expect the above statment to return the value of second "a" element but instead it returns the value of all "a" elements in the page. I cant understand Why?
what you have shall return first a-child of every element.
so //a[1] will work as follows (result will be 2 nodes):
X
Y
a <-- give you this
a
Z
a <-- and this
a
try (//a)[1] instead
That XPATH expression looks for all a elements at every level of the document and the predicate filter selects the first a at every step.
So, depending on how your XML is structured, you might not get every a element (if there were more than one a that were siblings, you would only get the first one of those siblings).
However, if you intended to just select the first a in the document, you could use this expression: (//a)[1]
Wrapping the selection of //a in the parenthesis creates a collection that the predicate filter is then applied, selecting the first in the collection, rather than the first a encountered at each step of the //.