Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old January 10th, 2011, 03:40 PM
Registered User
 
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to access XML elements one at a time using xslt?

Hello,

Code:
This is my XML document.
<?xml version="1.0"?>
<company>
    <employee>
        <firstname>Tom</firstname>
        <middlename>WWW</middlename>
        <lastname>Cruise</lastname>
    </employee>
    <employee>
        <firstname>Paul</firstname>
        <lastname>Enderson</lastname>
    </employee>
</company>

Below is my XSLT which I have created.

<?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:template match="/">
           <xsl:call-template name="subtree">
            <xsl:with-param name="parents" select="/"/>
           </xsl:call-template>
        <xsl:copy-of select="."/>
       
    </xsl:template>      
    <xsl:template name="subtree">
        <xsl:param name="parents"/>
        <xsl:for-each select="$parents/*">
            <xsl:variable name="current-group" select="$parents/*[name() = name(current())]"/>
            <xsl:if test="count($current-group[1]|.) = 1">
                <xsl:call-template name="subtree">
                        <xsl:with-param name="parents" select="$current-group"/>
                    </xsl:call-template>   
            </xsl:if>
        </xsl:for-each>
   </xsl:template>   
</xsl:stylesheet>

OUTPUT
<?xml version="1.0" encoding="UTF-8"?><company>
    <employee>
        <firstname>Tom</firstname>
        <middlename>WWW</middlename>
        <lastname>Cruise</lastname>
    </employee>
    <employee>
        <firstname>Paul</firstname>
        <lastname>Enderson</lastname>
    </employee>
</company>
My requirement is to design a stylesheet which when applied to any XML document should access each XML element at a time so that I can use it further for creating another XML document as per my requirement. Could you please let me know how to do this? Once I am able to access particular element, is it possible to know who all are its child elements?

Thanks in advance,
Komal
 
Old January 10th, 2011, 04:12 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You just described XSLT - that is what it does.

The only line in your XSLT above that does anything is <xsl:copy-of select="."/> - the rest is just junk.

Perhaps if you could give us an example of what you WANT the output too look like then we might be able to help you.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 10th, 2011, 05:04 PM
Registered User
 
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
You just described XSLT - that is what it does.

The only line in your XSLT above that does anything is <xsl:copy-of select="."/> - the rest is just junk.

Perhaps if you could give us an example of what you WANT the output too look like then we might be able to help you.
Hello,

My requirement is to design a stylesheet which will work with any XML document. Something similar to generalized stylesheet.
For eg.
If the above XML document is used as input for XSLT for the first time, it should produce the same XML document again. But second time, if I use another XML document with different structure as input, the same stylesheet should get applied and produce XML document. In order to this,I was trying to access one XML element at a time rather than the entire XML structure. How is this possible?

I hope this clarifies my requirement.

Thanks,
Komal
 
Old January 11th, 2011, 05:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm sorry, you still aren't making your requirements clear.

If all the stylesheet does is output whatever is input then I see the point. If this is not what you mean then you need to provide a concrete example of where the output differs from the input.

Perhaps look up the "identity xslt template" will give you some clues, I don't know.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 11th, 2011, 08:29 PM
Registered User
 
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
I'm sorry, you still aren't making your requirements clear.

If all the stylesheet does is output whatever is input then I see the point. If this is not what you mean then you need to provide a concrete example of where the output differs from the input.

Perhaps look up the "identity xslt template" will give you some clues, I don't know.
Hi Sam,

Thanks for your reply. Please see below my actual requirement.

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.1. You should have one stylesheet which will function in the way described on any input XML
document.

Could you provide the stylesheet which can be applied to any XML document?

I am getting confused with accessing the elements since I have not worked much on XSLT.

Thanks in advance,
TechCarol
 
Old January 12th, 2011, 06:07 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

As I keep saying, could you give us an example input XML and what you want to be output.

We don't know how you want you java code to look, what libraries or jar files you might be using, where you want to store the output from the java, any number of other things.

I've already said to look up the identity template: http://www.dpawson.co.uk/xsl/sect2/identity.html

You are probably interested in looking up information on the node() and text() functions, and the @* and * xpath axis abbreviations.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 12th, 2011, 09:20 AM
Registered User
 
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
As I keep saying, could you give us an example input XML and what you want to be output.

We don't know how you want you java code to look, what libraries or jar files you might be using, where you want to store the output from the java, any number of other things.

I've already said to look up the identity template: http://www.dpawson.co.uk/xsl/sect2/identity.html

You are probably interested in looking up information on the node() and text() functions, and the @* and * xpath axis abbreviations.
This is my input XML file and output should be also the same.
<?xml version="1.0"?>
<company>
<employee>
<firstname>Tom</firstname>
<middlename>WWW</middlename>
<lastname>Cruise</lastname>
</employee>
<employee>
<firstname>Paul</firstname>
<lastname>Enderson</lastname>
</employee>
</company>

The main requirement is designing of stylesheet in such a way that if my input XML file changes, the same stylesheet when applied to the new XML document, should produce java code which in turn will produce the same XML file as output.

Hope now the requirement is clear.

Let me know if you need any more information.
 
Old January 12th, 2011, 10:04 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I think I am being generous here, but I will ask for the third time.

Show us exactly what you want your output to look like for a given input. DO NOT just describe it again, actually show us the java code you want to see!
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 12th, 2011, 11:22 AM
Registered User
 
Join Date: Jan 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
I think I am being generous here, but I will ask for the third time.

Show us exactly what you want your output to look like for a given input. DO NOT just describe it again, actually show us the java code you want to see!
Hello,

Please see below java code which needs to be generated when XSLT is transformed.

[CODE]
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
{

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
Element childElement=doc.createElement("employee");
company.appendChild(childElement);
}
}
 
Old January 12th, 2011, 11:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>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.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering XML Elements using XSLT. alapati.sasi XSLT 5 March 23rd, 2009 11:56 AM
XSLT: ONE template to transform all the elements Behl_Neha XSLT 8 December 15th, 2007 07:31 PM
renumbering xml elements with XSLT csbdeady XSLT 5 July 27th, 2005 09:17 AM
adding elements in XSLT 1.0 spencer.clark XSLT 3 July 25th, 2005 09:41 AM
Grouping Elements using XSLT mathuranuj XSLT 2 June 21st, 2005 02:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.