-2

I am trying to transform one form of XML to another using xslt and sax parser. Here are my sample inputxml and xsl snippets.

Input XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<AXL>
    <ROW>
       <firsname>John</firstname>
       <lastName>Smith</Smith>
    </ROW>
    <ROW>
       <firstname>George</firstname>
       <lastName>Tack</Smith>
    </ROW>
</AXL>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:template match="text()">
            <xsl:value-of select='normalize-space()'/>
        </xsl:template>
        <xsl:template match="/">
        <!--bom:AML-->
        <xsl:for-each select="//row">
                         <Contact>
            <xsl:attribute name="firstName"><xsl:value-of select="firstName"/></xsl:attribute>
            <xsl:attribute name="lastName"><xsl:value-of select="lastName"/></xsl:attribute>
             </Contact>
                    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

I could generate the output.xml using online tools. But with java code, i am just getting an empty output file. I know I messed it up somewhere but don't know where??

I appreciate your help!

Ok, here is my java code. And it is <firstName>. Sorry, typo error.

 System.setProperty("javax.xml.transform.TransformerFactory",  
                        "net.sf.saxon.TransformerFactoryImpl");     

                TransformerFactory factory = TransformerFactory.newInstance();

                Templates template = factory.newTemplates(
                           new StreamSource(new FileInputStream("C:\\XML_Transformation.xsl")));
                        Transformer transformer = template.newTransformer();
                    Source source = new StreamSource(new FileInputStream("C:\\Sample.xml"));
                    Result result = new StreamResult(new FileOutputStream("C:\\Final.xml"));
                    transformer.transform(source, result);
newbie
  • 1
  • 1
  • adding a relevant snippet of your Java code would help people giving you feedback without having to come up with a solution by themselves. – Sigma Sep 18 '14 at 14:43
  • Well if the input really has `firsname` and the XSLT has `firstName` then one problem is the different spelling of the markup compared to the XPath expression. If you think your Java code matters then post it. – Martin Honnen Sep 18 '14 at 14:43
  • We're not even getting as far, because the input XML is malformed to begin with. I very seriously doubt that you could "generate the output" with any online tool. Any decent tool will not accept this as input. – Mathias Müller Sep 18 '14 at 14:49
  • Please show the actual XML you are trying to process. Your sample is clearly nonsense (e.g. end tag ``). Perhaps your problem is that the element name is "ROW" (uppercase) and your stylesheet is trying to match "row" (lowercase); but there are so many errors here that this is wild speculation. – Michael Kay Sep 20 '14 at 14:38

1 Answers1

0

As already mentioned in the comments, both your XML and XSLT wouldn't generate the desired output. The XML isn't valid, the XSLT has some other issues. Because this can be easily fixed (so you can proceed testing if your Java will work better) find repaired XML here:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<AXL>
  <ROW>
   <firstName>John</firstName>
   <lastName>Smith</lastName>
  </ROW>
  <ROW>
   <firstName>George</firstName>
   <lastName>Tack</lastName>
  </ROW>
</AXL>  

and working XSLT here:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:template match="/">
  <xsl:for-each select="//ROW">
    <Contact>
        <xsl:attribute name="firstName">
           <xsl:value-of select="firstName"/>
        </xsl:attribute>
        <xsl:attribute name="lastName">
           <xsl:value-of select="lastName"/>
        </xsl:attribute>
    </Contact>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>   

Result:

<?xml version="1.0" encoding="UTF-8"?>
<Contact firstName="John" lastName="Smith"/>
<Contact firstName="George" lastName="Tack"/>  

As additional information - it's also possible to have your template just matching the ROW, avoiding an unnecessary for-each loop:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:template match="ROW">
<Contact>
   <xsl:attribute name="firstName">
      <xsl:value-of select="firstName"/>
   </xsl:attribute>
   <xsl:attribute name="lastName">
      <xsl:value-of select="lastName"/>
   </xsl:attribute>
</Contact>
</xsl:template>
</xsl:stylesheet>

There are plenty of resources online for XSLT, but for starters you can check some good questions and detailed answers given here: XSL xsl:template match="/" and here: In what order do templates in an XSLT document execute, and do they match on the source XML or the buffered output?

Community
  • 1
  • 1
matthias_h
  • 11,356
  • 9
  • 22
  • 40