如何对此表达式进行格式设置,以便在for循环运行时仅选择分析属性为空的位置。原因是有多个具有相同值的templateNames。这是我的尝试,但不能去上班Xpath仅选择具有空值的属性
String theXpath = "//report-plan[@name='"+ templateName +"']/settings/@analysis=''";
示例代码:
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();
}
}
}
示例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>
目前我的代码输入两个相同的值报告命名为通用。即使分析属性具有不同的输入值。即使报告名称相同,我也需要输入不同分析值的代码。
输出:
<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样本
TEMPLATE_NAME ANALYSIS_NAME PARAM_NAME
generic analval1 paramval1
generic analval2 paramval2
sensitivity analval3 paramval3
有什么不对您当前的XPath? – har07
只有在“分析”属性为空时才选择_what exactly_? –
只在该报告计划元素中选择该分析属性。有多个报告计划元素,其中一些具有值,另一些是空的,所以我只选择那些空的。已更新以显示相关代码。 – Kelv