Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Validating XML with XML schema (XSD)

Guys believe me today I struggled a lot trying to do a very simple thing: Validating an xml file with my XSD( XML Schema), using a SAX parser(apache). I tried simple standalone program and in one shot I got itworking and now I want it to inside my web application. I added it(XSD)to the resources(classpath) and deployed my app to the tomcat but unfortunately I started getting the error "cvc-elt.1: Cannot find the declaration of element 'Root'". So I started checking my input xml m the XSD and all. All is well formed and Root is there. Then question remains where I went wrong. After some observation I found that the parser(com.sun.org.apache.xerces.internal.parsers.SAXParser) not able to get the XSD itself :( . I checked my project structure and but all things are at place. So I started putting the XSD in possible locations where this guy will look. But no luck. By now I'm pretty sure that culprit is not the directory structure. After some internet search I found thatparser properties value should be URL but I didn't want the XSD to be publicly available. Unfortunately, I didn't find how to do that directly on the Internet. So after trying things ,I came to know that which I didn't knew earlier the simple thing I missed it is that JAVA can generate an URL pointing to a file residing in the classpath. so here's the code :

SAXBuilder builder = new SAXBuilder("com.sun.org.apache.xerces.internal.parsers.SAXParser",true);
builder.setFeature("http://xml.org/sax/features/validation", true);
builder.setFeature("http://apache.org/xml/features/validation/schema",true);
builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("/my.properties"));
URL schemaUrl = getClass().getResource(properties.getProperty("my.xsd"));
builder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",schemaUrl);
builder.build(inXML);

I haven't used any ErrorHandler since just want to show the exact error message coming from XSD validation and halt. Good luck!!

Posted at at 6:24 AM on Friday, January 14, 2011 by Posted by Ravindra Nikam | 0 comments   | Filed under: ,

java.lang.NoClassDefFoundError: org/jaxen/JaxenException

If you see in my new project, there is going to be a lot of XML processing. So I was wondering what would be the better approach to parse the XML files with JAVA. After some R&D I came across a very nicely written API's for parsing the XML and you can go back and forth to get any node since each entity in xml file has been considered as object, so no more restrictions.Yes, I am talking about JDOM. It also comes with very good utility class to process xml using XPath. The static methods provided by class org.jdom.xpath.XPath are pretty straight forward and simple to use in one line :

Listlist = XPath.selectNodes(context, "//root"); 
Object obj = XPath.selectSingleNode(context, "//root"); 
This will return you the list/Element of matching node(s) as a object. So when I were trying this first time I just started with downloading JDOMv1.1.1.jar from the web.But unfortunately I got stuck with the this wierd exception
java.lang.NoClassDefFoundError: org/jaxen/JaxenException ... 
. So solution is: putting the jdom jar itself not resolve this problem.Since JDOM uses "Jaxen engine" for XPath processing you need to add one more jar called jaxen.jar along with it from here.

Posted at at 9:35 PM on Thursday, December 23, 2010 by Posted by Ravindra Nikam | 0 comments   | Filed under: , ,

removing empty attributes

XSLT template to remove empty attributes from the xml.

<xsl:template match="/">
  <xsl:copy>
     <xsl:apply-templates>
       <xsl:for-each select="@*">
          <xsl:if test=".!=''">
             <xsl:copy-of select="." />
          </xsl:if>
       </xsl:for-each>
     </xsl:apply-templates>
   </xsl:copy> 
</xsl:template>

Posted at at 4:21 AM on Saturday, October 2, 2010 by Posted by Ravindra Nikam | 0 comments   | Filed under: , , ,

Sorting/Reordering elements using xslt

XSLT template to reorder the elements as per the position. The order option allows you to decide the order of sort. Depending on the select XPath query result data-type can be given.

<xsl:template match="*">
    <xsl:copy>
       <xsl:apply-templates>
          <xsl:sort select="position()"order="descending" data-type="number" />
       </xsl:apply-templates>
     </xsl:copy>
  </xsl:template>

Posted at at 6:03 AM on Friday, October 1, 2010 by Posted by Ravindra Nikam | 0 comments   | Filed under: , , ,

copy elements using xslt

XSLT template to just copy all elements without any condition from one file to another.

<xsl:template match="* | @*">
           <xsl:copy>
              <xsl:copy-of select="@*" />
              <xsl:apply-templates />
           </xsl:copy>
        </xsl:template>

Posted at at 5:57 AM on by Posted by Ravindra Nikam | 0 comments   | Filed under: , , ,

xml to xml conversion using xslt

As I am new to XSLT wanted to perform some xsl transformation on my xml file and generate the xml. But when I were looking for some example how to do it I was getting all example who teach you how to transform an xml to html. So here is the way I found after some web search. Look at the tag <xsl:output> and its method just set it to xml as below. The code snippet to just copy all the elements and attributes from one xml file to another. If you want to perform some operations or checks you are free to add new templates.

<xsl:stylesheet version='1.0' 
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> 

<xsl:output method='xml' indent='yes' /> 

<!-- copy all elements which don't match any template --> 
  <xsl:template match="* | @*"> 
    <xsl:copy> 
       <xsl:copy-of select="@*" /> 
       <xsl:apply-templates /> 
    </xsl:copy> 
  </xsl:template> 
</xsl:stylesheet>

Posted at at 6:19 AM on Thursday, September 30, 2010 by Posted by Ravindra Nikam | 0 comments   | Filed under: , , ,