 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 12th, 2011, 02:34 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mhkay
>To write a stylesheet which, when you apply it to an XML document, produces Java code as output: this Java code, when you run it, should (using the DOM libraries) output an XML document which will be the same as the XML document you started with.
OK, I've finally understood what you are trying to do. But I don't understand why! The simplest Java code to generate is surely code that takes the XML document as a character string and calls the DOM's parse() method.
To what extent is it actually necessary to do this by generating calls on DOM methods such as createElement() and appendChild()? For example, would it be equally acceptable to generate methods representing SAX events such as startElement() and endElement(), so that the Java program builds the DOM using a SAX-to-DOM builder?
If you want to do it the way you have suggested, it's certainly possible. The Java program might get quite large, and if the XML is more than 10Kb or so, I wouldn't be surprised if the generated Java breaks compiler limits. It doesn't need many template rules either - one per node kind should be fine. Something like this (using generate-id() to produce variable names that refer to the nodes):
Code:
<xsl:template match="*">
Element <xsl:value-of select="generate-id()"/> = doc.createElement("<xsl:value-of select="name()"/>");
<xsl:value-of select="generate-id(..).appendChild(<xsl:value-of select="generate-id()"/>);
<xsl:apply-templates select="@*/>
<xsl:apply-templates/>
</xsl:template>
and then similar templates to match attribute nodes, text nodes, and so on.
|
Thanks for the reply, michael.
This is part of my assignment which needs to be submitted. We have to use only DOM methods and not SAX. Also, I had tried doing it the same way as you have suggested that takes the XML document as a character string and calls the DOM's parse() method. But this is not accepted by my instructor. They want me to design stylesheet which traverses through the XML document and also handles recursion and finally generate java code. So I need to try using the code suggested by you and
|
|

January 12th, 2011, 02:35 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mhkay
>To write a stylesheet which, when you apply it to an XML document, produces Java code as output: this Java code, when you run it, should (using the DOM libraries) output an XML document which will be the same as the XML document you started with.
OK, I've finally understood what you are trying to do. But I don't understand why! The simplest Java code to generate is surely code that takes the XML document as a character string and calls the DOM's parse() method.
To what extent is it actually necessary to do this by generating calls on DOM methods such as createElement() and appendChild()? For example, would it be equally acceptable to generate methods representing SAX events such as startElement() and endElement(), so that the Java program builds the DOM using a SAX-to-DOM builder?
If you want to do it the way you have suggested, it's certainly possible. The Java program might get quite large, and if the XML is more than 10Kb or so, I wouldn't be surprised if the generated Java breaks compiler limits. It doesn't need many template rules either - one per node kind should be fine. Something like this (using generate-id() to produce variable names that refer to the nodes):
Code:
<xsl:template match="*">
Element <xsl:value-of select="generate-id()"/> = doc.createElement("<xsl:value-of select="name()"/>");
<xsl:value-of select="generate-id(..).appendChild(<xsl:value-of select="generate-id()"/>);
<xsl:apply-templates select="@*/>
<xsl:apply-templates/>
</xsl:template>
and then similar templates to match attribute nodes, text nodes, and so on.
|
Thanks for the reply, michael.
This is part of my assignment which needs to be submitted. We have to use only DOM methods and not SAX. Also, I had tried doing it the same way as you have suggested that takes the XML document as a character string and calls the DOM's parse() method. But this is not accepted by my instructor. They want me to design stylesheet which traverses through the XML document and also handles recursion and finally generate java code. So I need to try using the code suggested by you. I shall keep you posted about how do I go about it.
|
|

January 12th, 2011, 02:50 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>This is part of my assignment which needs to be submitted. We have to use only DOM methods and not SAX.
Sadly, in your career as a programmer you will often be given requirements to do things in a particular way, and I hope you will learn to ask the question "why?", to challenge what you have been told, to discover the real underlying requirements, and to propose better ways of meeting them. When you are a student, though, the underlying requirement is often to learn about a particular technology whether or not it is the most appropriate one for the task, so you have to put up with it!
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

January 13th, 2011, 06:54 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mhkay
>This is part of my assignment which needs to be submitted. We have to use only DOM methods and not SAX.
Sadly, in your career as a programmer you will often be given requirements to do things in a particular way, and I hope you will learn to ask the question "why?", to challenge what you have been told, to discover the real underlying requirements, and to propose better ways of meeting them. When you are a student, though, the underlying requirement is often to learn about a particular technology whether or not it is the most appropriate one for the task, so you have to put up with it!
|
Hello Michael,
What you mentioned in your last post regarding task of a student is very true. I did try writing the stylesheet which traverses through each and every element of the XML and also the attributes and text nodes and as mentioned by you, it did generate large amount of code. Could you please let me know if I am doing correctly? Is there a way to optimize this code further or this is how it should be done? Please suggest.
XML Document
<?xml version="1.0" encoding="UTF-8"?>
<company>
<staff id="1">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
Please see below stylesheet designed which takes any XML document as input and produces Java code.
NOTE: I have not yet completed the Java code as part of stylesheet. Only traversing of elements is taken care.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class TestElements
{
public static void main(String[] argv) throws Exception
{
</xsl:text>
<xsl:text>
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
</xsl:text>
<!--<xsl:apply-templates select="/"/>-->
<xsl:call-template name="CreateElement">
<xsl:with-param name="parents" select="/"/>
</xsl:call-template>
<xsl:text>
}
}
</xsl:text>
</xsl:template>
<xsl:template name="CreateElement">
<xsl:param name="parents"/>
<xsl:text>Element rootElement,childElement,GrandChildElement;</xsl:text>
<xsl:text>rootElement = doc.createElement("</xsl:text><xsl:value-of select="/*/local-name()"/><xsl:text>");
doc.appendChild(rootElement);
</xsl:text>
<xsl:variable name="countChild" select="count(/*/*) "/>
<xsl:variable name="childTextNode" select="/*"/>
<xsl:variable name="counter" select="abs(1)"></xsl:variable>
<xsl:if test="$countChild > 0">
<xsl:for-each select="/*/*">
<xsl:text>childElement=doc.createElement("</xsl:text><xsl:value-of select="name()"/><xsl:text>");</xsl:text>
<xsl:text>rootElement(</xsl:text><xsl:value-of select="$counter"/><xsl:text>).appendChild(childElement);</xsl:text>
<xsl:variable name="childAttribute" select="//*"/>
<xsl:if test="not($childAttribute/@*=' ')">
<xsl:text>Attr attr = doc.createAttribute("</xsl:text><xsl:value-of select="@*/name()"/><xsl:text>");
attr.setValue("</xsl:text><xsl:value-of select="@*"/><xsl:text>");</xsl:text>
<xsl:text>childElement.setAttributeNode(attr);</xsl:text>
</xsl:if>
<xsl:if test="not($childTextNode/text()=' ')">
<xsl:text>childElement.appendChild(doc.createTextNode("</xsl:text><xsl:value-of select="text()"/><xsl:text>");</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:variable name="countGrandChild" select="count(/*/*/.)"/>
<xsl:variable name="GrandChildAttribute" select="/*/*/*"/>
<xsl:variable name="grandTextNode" select="/*/*"/>
<xsl:if test="$countGrandChild > 0">
<xsl:for-each select="/*/*/*">
<xsl:text>GrandChildElement=doc.createElement("</xsl:text><xsl:value-of select="name()"/><xsl:text>");</xsl:text>
<xsl:text>childElement.appendChild(GrandChildElement);</xsl:text>
<xsl:if test="not($GrandChildAttribute/@*=' ')">
<xsl:text>Attr attr = doc.createAttribute("</xsl:text><xsl:value-of select="@*/name()"/><xsl:text>");
attr.setValue("</xsl:text><xsl:value-of select="@*"/><xsl:text>");</xsl:text>
<xsl:text>GrandChildElement.setAttributeNode(attr);</xsl:text>
</xsl:if>
<xsl:if test="not($grandTextNode/text()=' ')">
<xsl:text>childElement.appendChild(doc.createTextNode("</xsl:text><xsl:value-of select="text()"/><xsl:text>");</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Last edited by Techcarol; January 13th, 2011 at 07:24 PM..
Reason: Included stylesheet design instead of Java code
|
|

January 13th, 2011, 07:35 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You seem to have coded this to handle a fixed depth of hierarchy - you're processing levels 1, 2, and 3 separately and your code doesn't extend to arbitrary depth. I would expect to see it handle an arbitrary number of levels using recursive descent. The problem can surely be tackled in a stylesheet that uses <xsl:template match="*"> to process an element at any level and descend to its children using <xsl:apply-templates>, passing parameters as necessary.
The solution isn't going to scale to documents of any size because of the Java compiler limits; but I guess that doesn't matter for a schoolroom exercise. But getting the structure right is important, I think, and that means recursive descent.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

January 15th, 2011, 10:53 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mhkay
You seem to have coded this to handle a fixed depth of hierarchy - you're processing levels 1, 2, and 3 separately and your code doesn't extend to arbitrary depth. I would expect to see it handle an arbitrary number of levels using recursive descent. The problem can surely be tackled in a stylesheet that uses <xsl:template match="*"> to process an element at any level and descend to its children using <xsl:apply-templates>, passing parameters as necessary.
The solution isn't going to scale to documents of any size because of the Java compiler limits; but I guess that doesn't matter for a schoolroom exercise. But getting the structure right is important, I think, and that means recursive descent.
|
Hi Michael,
I have understood what needs to be done but I am still not able to put that in xslt. How will I be able to access each element at a particular level and then check whether it is element or text or attribute?
|
|

January 16th, 2011, 06:30 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>How will I be able to access each element at a particular level and then check whether it is element or text or attribute?
The standard XSLT design pattern is that when you're processing an element, you do what's necessary for that element, and then you process its attributes and/or children using
Code:
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
This activates templates that match the attributes, elements, and text nodes at the next level, rules with match patterns like
Code:
<xsl:template match="*">
which matches all elements, and
Code:
<xsl:template match="text()">
which matches text nodes. You never need to check whether the current node is an element or a text node, because different rules get fired for different kinds of node.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

January 16th, 2011, 09:30 AM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mhkay
>How will I be able to access each element at a particular level and then check whether it is element or text or attribute?
The standard XSLT design pattern is that when you're processing an element, you do what's necessary for that element, and then you process its attributes and/or children using
Code:
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
This activates templates that match the attributes, elements, and text nodes at the next level, rules with match patterns like
Code:
<xsl:template match="*">
which matches all elements, and
Code:
<xsl:template match="text()">
which matches text nodes. You never need to check whether the current node is an element or a text node, because different rules get fired for different kinds of node.
|
Thanks for the suggestion, michael.
But I am still not able to write the xslt :(. Could you please share sample code for me?
Code:
<company>
<employee no="1">Details
<firstname initial="T">Tom</firstname>
<middlename/>
<lastname>Cruise</lastname>
</employee>
<employee no="2">
<firstname initial="P">Paul</firstname>
<lastname>Enderson</lastname>
</employee>
</company>
|
|

January 16th, 2011, 09:54 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Here is some example XSLT 2.0 code (using XSLT 2.0 as to create text node you need some way to escape line breaks and double quotes to meet the Java syntax rules, that is easy in XSLT 2.0 with the replace function):
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf">
<xsl:output method="text"/>
<xsl:function name="mf:escape-java-string">
<xsl:param name="input" as="xs:string"/>
<xsl:sequence select="replace(replace($input, '\n', '\\n'), '"', '\\"')"/>
</xsl:function>
<xsl:template match="/">
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
public class MakeDocument
{
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document <xsl:value-of select="generate-id()"/> = db.newDocument();
<xsl:apply-templates/>
System.out.println(((DOMImplementationLS)<xsl:value-of select="generate-id()"/>.getImplementation()).createLSSerializer().writeToString(<xsl:value-of select="generate-id()"/>));
}
}
</xsl:template>
<xsl:template match="*">
Element <xsl:value-of select="generate-id()"/> = <xsl:value-of select="generate-id(/)"/>.createElementNS("<xsl:value-of select="namespace-uri()"/>", "<xsl:value-of select="name()"/>");
<xsl:value-of select="generate-id(..)"/>.appendChild(<xsl:value-of select="generate-id()"/>);
<xsl:apply-templates select="@* | node()"/>
</xsl:template>
<xsl:template match="@*">
<xsl:value-of select="concat(generate-id(..), '.setAttributeNS("', namespace-uri(), '", "', name(), '", "', mf:escape-java-string(.), '"); ')"/>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="concat(generate-id(..), '.appendChild(', generate-id(/), '.createTextNode("', mf:escape-java-string(.), '")); ')"/>
</xsl:template>
</xsl:stylesheet>
I hope the code sample will be displayed correctly, unfortunately these forums tend to mess with numeric character references like & # 1 0; (only without the spaces) when inserted in code samples.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Last edited by Martin Honnen; January 16th, 2011 at 09:57 AM..
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

January 16th, 2011, 09:56 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Sigh, the forum has messed up the code sample where I used '& # 1 0;' (only without the spaces) so where you see XPath string literals starting on one line but continueing on the next the code is simply supposed to contain '& # 1 0;'.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
 |