How do I format this expression so that when a for-loop is running it only selects where analysis attributes are empty. The reason is that there are multiple templateNames with the same values. This is my attempt but can't get to work
String theXpath = "//report-plan[@name='"+ templateName +"']/settings/@analysis=''";
Sample code:
public class XPathTestReports {
public static void main(String[] args) {
try {
String outputFile = "c:/workspace/samplenew.xml";
String inputFile = "c:/workspace/sample.xml";
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new InputSource(inputFile));
// locate the node(s)
XPath xpath = XPathFactory.newInstance().newXPath();
// lOAD THE File
CSVImporterReports loader = new CSVImporterReports("C:/REPORT_TEMPLATES.csv");
List < OnConfig > entries = loader.getEntries();
for (OnConfig c: entries) {
String templateName = c.getTemplateName();
String analName = c.getAnalysisName();
String paramName = c.getParamName();
String theXpath = "//report-plan[@name='" + templateName + "']/settings/@analysis=''";
NodeList nodes = (NodeList) xpath.evaluate(theXpath, doc, XPathConstants.NODESET);
// make the change
for (int i = 0; i < nodes.getLength(); i++) {
nodes.item(i).setTextContent(analName);
// nodes.item(i).setTextContent(paramName);
}
}
try {
// save the result
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
} catch (TransformerException e) {
// TODO Auto-generated catch block
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
}
Sample XML:
<report-plan name="generic">
<columns>
<column name="Nominal" subtotal-function="Sum" total-function="Sum"/>
<column name="Trade"/>
</columns>
<settings analysis="" analysisParameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/>
</report-plan>
<report-plan name="generic">
<columns>
<column name="Nominal" subtotal-function="Sum" total-function="Sum"/>
<column name="Trade"/>
</columns>
<settings analysis="" analysisParameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/>
</report-plan>
<report-plan name="sensitive">
<columns>
<column name="Nominal" subtotal-function="Sum" total-function="Sum"/>
<column name="Trade"/>
</columns>
<settings analysis="" analysisParameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/>
</report-plan>
At the moment my code is inputting the same value for both reports named generic. Even though the analysis attributes have different values to be entered. I need the code to enter in different analysis values even if the report names are the same.
Output:
<report-plan name="generic">
<columns>
<column name="Nominal" subtotal-function="Sum" total-function="Sum"/>
<column name="Trade"/>
</columns>
<settings analysis="newValue" analysisParameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/>
</report-plan>
<report-plan name="generic">
<columns>
<column name="Nominal" subtotal-function="Sum" total-function="Sum"/>
<column name="Trade"/>
</columns>
<settings analysis="newValue" analysisParameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/>
</report-plan>
<report-plan name="sensitive">
<columns>
<column name="Nominal" subtotal-function="Sum" total-function="Sum"/>
<column name="Trade"/>
</columns>
<settings analysis="someValue" analysisParameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/>
</report-plan>
CSV sample
TEMPLATE_NAME ANALYSIS_NAME PARAM_NAME
generic analval1 paramval1
generic analval2 paramval2
sensitivity analval3 paramval3
analysisattribute is empty?