Thursday, May 17, 2012

Validate XML against XML Schema using Java

Taking up the Employees.xsd schema and Employees.xml again for this illustration as well.
We want to validate the xml instance against the schema and want to achieve this through Java code. Not challenging, eh? Ageed, however thought I'd pen it here for future references.

Here's the code that achieves it -
public class SchemaValidation {

  private static void validateXML(File sourceFile, File schemaFile) throws IOException,SAXException {

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = schemaFactory.newSchema(schemaFile);

    Validator validator = schema.newValidator();
    try {
      validator.validate(new StreamSource(sourceFile));
      System.out.println("is valid");
    } catch (SAXException e) {
      System.out.println("not valid because " + e.getLocalizedMessage());
    }
  }

  public static void main(String args[]) throws ParserConfigurationException, IOException,
      SAXException {

    File file = new File("Employees.xml");
    File schemaFile = new File("Employees.xsd");
    SchemaValidation.validateXML(file, schemaFile);
  }
}
So when I try validating the XML below against the Employees xsd schema -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees">
    <tns:Employee>
        <tns:Name>tns:Name</tns:Name>
        <tns:ID>tns:ID</tns:ID>
        <tns:Designation>tns:Designation</tns:Designation>
        <tns:Dept1>tns:Dept</tns:Dept1>
    </tns:Employee>
</tns:Employees>

I get the output -
not valid because cvc-complex-type.2.4.a: Invalid content was found starting with element 'tns:Dept1'. One of '{"http://www.example.org/Employees":Dept}' is expected.

So you see, the XML Parser pin points the exact error location along with the resolution. So for the above case it expects a <Dept> tag whereas what I pass is <Dept1>. Hence, the error.
   


Wednesday, May 9, 2012

Append Nodes to a an existing XML via XSL

Many a times we have a need to manipulate an xml to append similar repeating nodes to it. This post addresses that need through a very simply written xsl.
For the context what I have here is an Employees xml schema as below -
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Employees" xmlns:tns="http://www.example.org/Employees"  elementFormDefault="qualified">

<element name="Employees">
<complexType>
<sequence>
<element name="Employee" type="tns:empType" minOccurs="1" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<complexType name="empType">
<sequence>
<element name="Name" type="string" />
<element name="ID" type="string" />
<element name="Designation" type="string" />
<element name="Dept" type="string" />
</sequence>
</complexType>
</schema>


I have an xml corresponding to the above schema as shown below -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<tns:Employee>
<tns:Name>Jack</tns:Name>
<tns:ID>1</tns:ID>
<tns:Designation>Developer</tns:Designation>
<tns:Dept>Java</tns:Dept>
</tns:Employee>
<tns:Employee>
<tns:Name>Bill</tns:Name>
<tns:ID>2</tns:ID>
<tns:Designation>Architect</tns:Designation>
<tns:Dept>SOA</tns:Dept>
</tns:Employee>
</tns:Employees>

All that I would be doing is copying the entire xml in its entirety and then appending an extra tag to it after the last one. The xsl for achieving this is as below -
<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.example.org/Employees"
exclude-result-prefixes="#default tns">

<xsl:template match="/">
<tns:Employees>
<xsl:for-each select="/tns:Employees/tns:Employee">
<xsl:copy-of select="." />
<xsl:if test="position()=last()">
<xsl:call-template name="append" /> </xsl:if>
</xsl:for-each>
</tns:Employees>
</xsl:template>
<xsl:template name="append">
<tns:Employee>
<tns:Name>Demo-Name</tns:Name>
<tns:ID>3</tns:ID>
<tns:Designation>Demo-Designation</tns:Designation>
<tns:Dept>Demo-Dept</tns:Dept>
</tns:Employee>
</xsl:template>
</xsl:stylesheet>


The template named 'append' contains the new <Employee> tag that needs to be appended.
So the output xml obtained after executing the above xsl with the Employees.xml as input is as below -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees">
<tns:Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:Name>Jack</tns:Name>
<tns:ID>1</tns:ID>
<tns:Designation>Developer</tns:Designation>
<tns:Dept>Java</tns:Dept>
</tns:Employee>
<tns:Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:Name>Bill</tns:Name>
<tns:ID>2</tns:ID>
<tns:Designation>Architect</tns:Designation>
<tns:Dept>SOA</tns:Dept>
</tns:Employee>
<tns:Employee>
<tns:Name>Demo-Name</tns:Name>
<tns:ID>3</tns:ID>
<tns:Designation>Demo-Designation</tns:Designation>
<tns:Dept>Demo-Dept</tns:Dept>
</tns:Employee>
</tns:Employees>
You see the new <Employee> appended right after the last <Employee> tag of the original xml.Hope it helps! 


Do let me know if you face problems with xml manipulation using xsl. I'll be happy to help :)