我使用Apache Xalan 2.7.1进行XSLT转换。
我尝试将input
转换为expected-output
。
输入
<customer>
<customer-name>Diecast Collectables</customer-name>
<phone xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<city>Boston</city>
</customer>
XSLT
<xsl:template match="node()[@xsi:nil = 'true']">
<xsl:copy>NULL</xsl:copy>
</xsl:template>
预期输出(请注意&#39; NULL&#39;值)
<customer>
<customer-name>Diecast Collectables</customer-name>
<phone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NULL</phone>
<city>Boston</city>
</customer>
但这是我用Xalan java库得到的输出。(注意那里没有&#39; NULL&#39;值)
<customer>
<customer-name>Diecast Collectables</customer-name>
<phone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></phone>
<city>Boston</city>
</customer>
但令人惊讶的是,上面的XSLT给出了this online tool的预期输出。
有人可以解释这种差异的原因吗?
谢谢, Bhathiya
[编辑]
Transfromation Code
public void transform() throws Exception {
OMElement omElemHeader = AXIOMUtil.stringToOM("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
" <customer>\n" +
" <customer-name>Diecast Collectables</customer-name>\n" +
" <contact-last-name>Franco</contact-last-name>\n" +
" <phone xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>\n" +
" <city>Boston</city>\n" +
" </customer>\n");
TransformerFactory tFactory = TransformerFactory.newInstance();
File xslt = new File("/data/xslt.xslt");
Transformer transformer = tFactory.newTransformer(
new StreamSource(new FileInputStream(xslt)));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new OMSource(omElemHeader);
transformer.transform(xmlSource, new StreamResult(outputStream));
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
XMLStreamReader reader2 = XMLInputFactory.newInstance().
createXMLStreamReader(inputStream);
StAXOMBuilder builder2 = new StAXOMBuilder(reader2);
OMElement out = builder2.getDocumentElement();
System.out.println(out);
}
答案 0 :(得分:0)
发现了这个问题。 xslt中的xsi
命名空间中存在拼写错误。 (我应该在问题中发布完整的xslt。: - /)
无论如何,谢谢所有试图帮助我的人。