i am currently doing java and html, i have to check some values from the website against my database. i would like to ask if XPATH has such function like "is text present" or "get text = data" rather than get element, is there something to check if the value is there or something?
You can use boolean method in XPath.
For example, lets say your XML is this
<?xml version="1.0" >
<persons>
<person>
<name>Peter</name>
<sex>MALE</sex>
<age>25</age>
</person>
</persons>
You can use the boolean method as,
boolean(/persons/person[name = 'Peter'])
Related
I have XML like:
<?xml version='1.0' encoding='UTF-8'?>
<ClinicalDocument xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='urn:hl7-org:v3'
xmlns:ext='urn:hl7-RU-EHR:v1'
xsi:schemaLocation='urn:hl7-org:v3'>
<author>
<time value='20160809000000+0300'/>
<assignedAuthor>
<id root='1.2.643.5.1.13.3.25.1.1.100.1.1.70' extension='1'/>
<id root='1.2.643.100.3' extension='03480134121'/>
<id nullFlavor='NI'/>
</assignedAuthor>
</author>
</ClinicalDocument>
I have to get extension in id with root's value = 1.2.643.100.3.
I must use XPath 2.0.
I have tried:
*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id' and #id='1.2.643.100.3']/#extension. Not working
/*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id'][2]/#extension, but order of ids can
mixed. So that, I should retrieve by id's value
It's needed to me for retrieving value by Java's XPathExpression
First, bind namespace prefix, u: to urn:hl7-org:v3.
Then, this XPath,
//u:id[#root='1.2.643.100.3']/#extension
will return 03480134121, as requested.
If you are unable to bind a namespace prefix, you can instead use this XPath,
//*[local-name() ='id' and #root='1.2.643.100.3']/#extension
which will also return 03480134121, as requested.
Correct XPath: /*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[local-name()='id' and #root='1.2.643.100.3']/#extension
I am new to Xpath. I am facing a problem that I have to get a boolean response from Xpath, if an element does not contains any text then it should return false otherwise true. I have seen many examples and I don't have much time to learn Xpath expressions. Below is the Xml file.
<?xml version="1.0" encoding="UTF-8" ?>
<order id="1234" date="05/06/2013">
<customer first_name="James" last_name="Rorrison">
<email>j.rorri#me.com</email>
<phoneNumber>+44 1234 1234</phoneNumber>
</customer>
<content>
<order_line item="H2G2" quantity="1">
<unit_price>23.5</unit_price>
</order_line>
<order_line item="Harry Potter" quantity="2">
<unit_price></unit_price>//**I want false here**
</order_line>
</content>
<credit_card number="1357" expiry_date="10/13" control_number="234" type="Visa" />
</order>
Could you point me the right direction to create xpath expression for this problem.
What I want is a expression(dummy expression) as below.
/order/content/order_line/unit_price[at this point I want to put a validation which will return true or false based on some check of isNull or notNull].
The following xpath will do this:
not(boolean(//*[not(text() or *)]))
but this xpath will also include the credit_card node since it to does not contain any text (the attributes are not text()).
if you also want to exclude node with attributes then use this..
not(boolean(//*[not(text() or * or #*)]))
Following your edit, you can do this..
/order/content/order_line/unit_price[not(text()]
It will return a list of nodes with no text and from there you can test against the count of nodes for your test.
or to return true/false..
not(boolean(/order/content/order_line/unit_price[not(text()]))
Having some trouble returning certain fields from a SharePoint List SOAP request.
Here is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soap1="http://schemas.microsoft.com/sharepoint/soap/">
<soap:Header/>
<soap:Body>
<soap1:UpdateListItems>
<soap1:listName>69A3FFFA-782B-45D5-B776-2BE6D5645745</soap1:listName>
<soap1:updates>
<Batch OnError="Continue">
<Method ID="1" Cmd="New">
<Field Name="Title">New Item</Field>
</Method>
</Batch>
</soap1:updates>
</soap1:UpdateListItems>
</soap:Body>
</soap:Envelope>
I am able to use the following Jdom2 code to grab certain values like this:
// set your name spaces.
Namespace soap = Namespace.getNamespace("soap","http://www.w3.org/2003/05/soap-envelope");
Namespace soap1 = Namespace.getNamespace("soap1","http://schemas.microsoft.com/sharepoint/soap/");
// drill down into elements
Element rootNode = doc.getRootElement();
// Get Body node
Element body = rootNode.getChild("Body",soap);
// Get UpdateListItem Element
Element UpdateListItems = body.getChild("UpdateListItems",soap1);
// Get updates node
Element updates = UpdateListItems.getChild("updates",soap1);
// Set list name as String variable
String listNameString = UpdateListItems.getChild("listName",soap1).getText();
// Print list text value ** THIS WORKS**
System.out.println(listNameString);
However, I can't seem to figure out how to select the Field elements.
For example: How would I select the "Title" Field?
<Field Name="Title">New Item</Field>
UPDATE:
I also able to get the attribute "Name" from the "Field" element, but can only return or set the name of value of the attribute. I need to be able to access the test within the "Field" Element.
I can get the value of the attribute like this:
System.out.println(field.getAttribute("Name").getValue()); // Prints Title
And I can get the name like this:
System.out.println(field.getAttribute("Name").getName()); // Prints Name
But, I need to be able to return the text value of the element.
UPDATE 2:
I didn't mention. The XML really looks like this:
` <?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soap1="http://schemas.microsoft.com/sharepoint/soap/">
<soap:Header/>
<soap:Body>
<soap1:UpdateListItems>
<soap1:listName>69A3FFFA-782B-45D5-B776-2BE6D5645745</soap1:listName>
<soap1:updates>
<Batch OnError="Continue">
<Method ID="1" Cmd="New">
<Field Name="Title">New Item</Field>
<Field Name="Classification" Type="Choice">Funny</Field>
<Field Name="Title">New Item</Field>
<Field Name="Title" Type="Text">Funny List Item</Field>
</Method>
</Batch>
</soap1:updates>
</soap1:UpdateListItems>
</soap:Body>
</soap:Envelope>`
I can submit this via SoapUI to SharePoint and it works. But if there are multiple "Field" elements, with different attributes, how can I select the correct one via Jdom2?
I can do this:
String title = field.getText(); //returns New Item
But how would I be able to grab the text from other "Field" elements that use the "Name" attribute?
It is all in the namespaces. You have three of them, soap, soap1, and there's also the default namespace, which, in this case, is "". JDOM designates this namespace as Namespace.NO_NAMESPACE.
So, to get the Field Element from the updates Element, you can do:
Element methods = updates.getChild("Method", Namespace.NO_NAMESPACE);
Element field = methods.getChild("Field", Namespace.NO_NAMESPACE);
These can be made simpler, if you want, by using the getChild method that does not have the namespace parameter at all, like:
Element methods = updates.getChild("Method");
Element field = methods.getChild("Field");
The important thing to see here, is that your document has 3 namespaces, and that the Field element (and Method too) are not in the soap, or soap1 namespace.
Thanks for the help rolfl. I figured it out. You can loop through the Child elements to access the different "Field" attributes. I then test for the attribute name to get or set its content. This is the best I could come up with.
for (Element node : method.getChildren("Field")){
if(node.getAttributeValue("Name").equalsIgnoreCase("Title")){
node.setText("String");
}
System.out.println(node.getAttribute("Name").getValue());
}
I want to store the below xml in a String variable:
<?xml version="1.0" encoding="utf-8"?>
<PublishData>
<PublishProfile
PublishMethod="AzureServiceManagementAPI"
Url="https://management.core.windows.net"
ManagementCertificate="MIIKFAIBAzCCCdQGCSqGSIb3DQEHAaCCCcUEggnBMIIJvTCCBe4GCSqGSIb3DQEHAaCCBd8EggXbMIIF1zCCBdMGCyqGSIb3DQEMCgECoIIE7jCCBOowHAYKKoZIhvcNAQwBAzAOBAjqVDJW6A1sCAICB9AEggTIVMsNIWg9llVa/HZ+rSFiqkszEWVSCEUlnG6pUlm9HEHaBP0w6a8pzPI9b/RoK0nWpUfpjX2gIOSiELDH4va+bDT4JA177GDorjM81jnMo4i+vjuzIrXd16pihFEoQbCICwcHX+qczRK/gMv+EPujL56+b1HfeDcRUePcJlUbtGf/STD7LH8/l5Ilm6CblzgP6RvX+wBWkS7215GpT3YpWXnzP8L16UAW/M9ifVC1aQogJecmUHrDLNw3QnFQK/NN0jRa0jhflFEAw8kIsAyVMTsSE1z+jH5loiDH1g89ywZyrBISJWnrotv8Xo5dsaDSSh51Cln6ANgAFVmXpYng0p4y1IDTXtM8a8isUj+nWMGFLjmh+rwn7GHM3nV3NvACsX6gokfrZQiCUv479KTYvFW2Yly6cJrG9dp24JcDeOgQbGHAyj14U4sRJ0+kkb/sY6OCZgJLHg0eJr2JR6Nmt9owrOeFPQiQUT6XPB8B7Lj57+rOzBkeeNbKSmS/ljhMdx9DvnYJELjrMstXIEfwuK5LSlJjR70UlVTjZeEbysD5D07tVYTx4a6dyvjYWGJT1UV2vXHEHE1HNoGICquIz9tCYbdKZUUVnZLJXUjNQ2igvNtuRDWsIO0GVzz/aE6e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1PG7dLiF7zQBurscq1hN98ZPSSyuOHRiq/2HHeuswaLjRkRUaf9sujdOJhVePIMG44D+SFNZ0w3vYTLyPa6jq5Xp2F7cYtfF5rKN3Cxdb5e2VLRYL9Tmr69ecrmOwLGqDo8m95SyI84Wv+QTKMvG7KY1nf71d4XuSHclHc+mflB2d+x1DIEiVI2AFWGYQzaGrCGYNg46ccY20WLC+GfWfh9kvdhw1uK+9zyreVU0nPFcVhbNWsFzCBzG/dAJTMhquvyebxbTMrqd2frZq6ANIQJRev/BpEN7HdCDOdIkJ25VXUsai8/EbGKSnoSQpMku5WdbhtMZBvbWf9bLMBb6CftSb3curLgRGpUvuKRXazMFIqSlFmMYTA3MB8wBwYFKw4DAhoEFFPRnQ+fTrJeB5vkx1qNHsIJDUfuBBRxnLPy8VCPymFot7wDaFcEn3qVqw==">
<Subscription
Id="36eccef3-558a-4b-9e14-4efcf57ab86c"
Name="Enterprise" />
<Subscription
Id="12501f03-5b8c-448d-a342-24c5f8"
Name="Development" />
and use
driver.findElement(By.id("ACPSIBDAzure.AccessKey")).sendKeys(/* String Variable */);
to pass xml content to a text input box. How can I do this using Java
You put your XML into a String variable. If you do not know how, perhaps this will help: Java multiline string
Then you reference that variable in your next .sendKeys() statement.
i have this XML document:
<?xml version="1.0" encoding="UTF-16"?>
<root>
<items>
<item1>
<tag1>1</tag1>
<tag2>2</tag2>
<tag3>3</tag3>
</item1>
<item2>
<tag1>4</tag1>
<tag2>5</tag2>
<tag3>6</tag3>
</item2>
</items>
</root>
I want to iterate the item elements (item1, item2...), and for each tag get the tag name and after that the value of the tag.
I am using DOM parser.
Any ideas?
Sorry, but this ain't an unsolvable or complicated problem, this is simply reading a tutorial which can be googled within seconds.
And of course, you might also check the documentation, which will give you a hint about this handy method called "getNodeName()".