I am trying to use an InDesign jsx file to read an xfdf file which has been exported from a form in Adobe Acrobat. I want to be able to find nodes by name attribute and return the value as a variable.
I am following the xpath syntax in XPath Syntax (w3schools.com) which seems quite simple.
I am able to get a response from xpath using wildcards ("//*) but as soon as I try to search for anything specific it returns nothing ("//field.value") or "//field[@name='File_1']/value".
I am sure I am close but cannot work out why these queries are coming back empty.
Below is a condensed version of my code with the XML data included as a variable for brevity.
At the end are various xpath commands and my comment on the outcome of eadch.
They can be un-commented to test.
try{
//content of the xfdf file is reproduced here:
var xmlFile = '''<?xml version="1.0" encoding="UTF-8"?>
><f href="Automation Form Test 1.pdf"
/><fields
><field name="File_1"
><value
>ABCDEF</value>
</field
><field name="File_2"
><value
>Some Text</value
></field
><field name="File_3"
><value
>Some more text</value
></field
></fields
><ids original="00054952DF51F14FB50D88332A014639" modified="B16CF70A22E4F341A0F65B6E7D370758"
/></xfdf
>''';
xmlFile.encoding ="UTF-8";
var xmlData = (XML(xmlFile));
//below returns all node values OK:
alert(xmlData.xpath("//*));
//below returns a node value by index OK:
//alert(xmlData.xpath("//*[1]));
//this gets all nodes and attributes OK:
//alert(xmlData.xpath("//node()"));
//this should get the value from all fields but returns nothing:
//alert(xmlData.xpath("//field.value"));
//this should get the value of the first 'field' node but returns nothing:
//alert(xmlData.xpath("//field[1].value"));
//below should return ABCDEF but returns nothing:
//alert(xmlData.xpath("//field[@name='File_1']/value"));
//tried an alternative method but it returns undefined is not an object:
//var xmlStuff = xmlData.xmlElements[0];
//var getData = xmlStuff.evaluateXPathExpression("//*");
//alert(getData);
}catch(error){
alert("Error" + error + "\nScript stopped.");
exit();
}