我现在正在研究这几天,我不知道为什么我无法让它发挥作用。我创建了一些java代码:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(xsl);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
try {
XPathExpression expr = xpath.compile("//xs:attribute-set[@name = \"tableImportantLine\"]/xs:attribute/text()");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
应该读取这个xsl-fo文件:
<?xml version="1.0" encoding="utf-8"?>
<xs:stylesheet xmlns:xs="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
<xs:attribute-set name="cell-style">
<xs:attribute name="border-width">0.5pt</xs:attribute>
<xs:attribute name="border-style">solid</xs:attribute>
<xs:attribute name="border-color">black</xs:attribute>
</xs:attribute-set>
<xs:attribute-set name="block-style">
<xs:attribute name="font-size"> 10pt</xs:attribute>
<xs:attribute name="line-height">15pt</xs:attribute>
<xs:attribute name="start-indent">1mm</xs:attribute>
<xs:attribute name="end-indent"> 1mm</xs:attribute>
</xs:attribute-set>
<xs:attribute-set name="thickText">
<xs:attribute name="font-weight">bold</xs:attribute>
</xs:attribute-set>
<xs:attribute-set name="tableImportantLine">
<xs:attribute name="background-color">#86f442</xs:attribute>
</xs:attribute-set>
<xs:attribute-set name="tableOutlineStyle">
<xs:attribute name="border">solid 0.5mm black</xs:attribute>
</xs:attribute-set>
<xs:attribute-set name="tableStyle">
<xs:attribute name="border">solid 0.1mm black</xs:attribute>
<xs:attribute name="text-align">center</xs:attribute>
</xs:attribute-set>
<xs:attribute-set name="tableHeaderStyle">
<xs:attribute name="border">solid 0.1mm black</xs:attribute>
<xs:attribute name="text-align">center</xs:attribute>
<xs:attribute name="font-weight">bold</xs:attribute>
</xs:attribute-set>
<xs:attribute-set name="stripedDesign">
<xs:attribute name="background-color">#123FC2</xs:attribute>
</xs:attribute-set>
</xs:stylesheet>
我删除了fo-templates,因为它们非常大并且编译没有问题(apache-fop)。 我已经检查了我的XPath,它应该可以完美地运行到#86f442&#39;。尽管如此,XPath选择器始终返回0个节点并且为空... 该文档包含xml。 感谢任何帮助过的人。
//编辑: 我还尝试直接选择&#39; // xs:attribute-set&#39;而不是更具体的xpath选择器。仍然只返回一个空节点列表。 使用
时也是如此String node = expr.evaluate(doc);
带
// xs:attribute-set [@name = \&#34; tableImportantLine \&#34;] / XS:属性/文本()
返回null ..