Pergunta

I'm using Apache Xalan 2.7.1 to do an XSLT transformation.

I'm trying to transform following input to expected-output.

input

<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>

expected-output (Note the 'NULL' value)

<customer>
   <customer-name>Diecast Collectables</customer-name>
   <phone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NULL</phone>
   <city>Boston</city>
</customer>

But this is the output I get with Xalan java library. (Note there's no 'NULL' value)

<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>

But surprisingly, above XSLT gives expected output in this online tool.

Can someone please explain what's the reason for this difference?

Thanks, Bhathiya

[EDIT]

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);
}
Foi útil?

Solução

Found the issue. There was a typo in xsi namespace in xslt. (I should have posted the complete xslt in the question. :-/ )

Anyway, Thanks all who tried to help me.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top