aspose file tools*
The moose likes XML and Related Technologies and the fly likes XML and XSL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML and XSL" Watch "XML and XSL" New topic
Author

XML and XSL

Amy Howard
Ranch Hand

Joined: Jul 11, 2002
Posts: 86
I wish somebody can advice me how to extract data from the elements and attributes in the given xml document which is validated by the following DTD. I read the examples provided by java.sun.com at http://developer.java.sun.com/developer/technicalArticles/xml/WebAppDev2/, but there is no examples to tell how to extract the data from the attributes of each of the elements.
Here is an DTD for the XML Doc:
<!DOCTYPE project [<!ELEMENT project ( command+) >
<!ATTLIST project name CDATA #REQUIRED >
<!ELEMENT command ( codeBase?, target*, requestparameter* ) >
<!ATTLIST command name NMTOKEN #REQUIRED >
<!ELEMENT codeBase ( #PCDATA ) >
<!ELEMENT target ( #PCDATA ) >
<!ATTLIST target name NMTOKEN #REQUIRED >
<!ELEMENT requestparameter ( #PCDATA ) >
<!ATTLIST requestparameter get ( true | false ) #REQUIRED >
<!ATTLIST requestparameter name NMTOKEN #REQUIRED >
<!ATTLIST requestparameter set ( true | false ) #REQUIRED >
<!ELEMENT sessionattribute ( #PCDATA ) >
<!ATTLIST sessionattribute get ( true | false ) #REQUIRED >
<!ATTLIST sessionattribute name NMTOKEN #REQUIRED >
<!ATTLIST sessionattribute set ( true | false ) #REQUIRED > ]>
Thanks!
[ July 26, 2002: Message edited by: Amy Howard ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
This post looks more likely to get answers in our lively and informative XML forum, so I've moved it for you.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
First, I would like to thank our kind, gallant and thoughtful sheriff Frank for providing me with opportunity to answer this question.
Amy, your DTD suggests that an XML instance documents will have the following structure:

To fetch content of elements, you can use <xsl:value-of> instruction:
<xsl:value-of select="project/command/codebase"/>
for attributes, you can use @attribName construction:
<xsl:value-of select="project/command/@name"/>
To get the full stylesheet, we need to add some housekeeping instructions:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="project/command/codebase"/>
<xsl:value-of select="project/command/@name"/>
</xsl:template>
</xsl:stylesheet>

Now you probably do not want to write full path to the elements of interest every time, and you may want to get more than one element, for this you need to use other XSLT instructions.
Here are two good tutorials:
http://www.vbxml.com/xsl/tutorials/intro/default.asp
http://www.vbxml.com/xsl/articles/xpath_xsl_style/default.asp
And here a lot of answers and recipes for typical tasks:
http://www.dpawson.co.uk/xsl/sect2/sect21.html
HTH
Amy Howard
Ranch Hand

Joined: Jul 11, 2002
Posts: 86
Mapraputa,
Thank you very much for your help. I have read the sites you suggested, and I will try to solve my question in XSL. But I don't know how to get the elements' names(project,command,codesBase,target,requestparameter) from the following xml file using XSL. I want to list the elements' names assciated with name attribute if it has in left frame, and display the detail information on each elements in the right frame when the user clicks on each nodes in the left frame. Can you help me with an example code? Thanks in advance!
<?xml version="1.0"?>
<project name="XML Generator Application">
<command name="View">
<codeBase>xmlgenerator.commands.View</codeBase>
<target name="success">/viewXMLDocument.jsp</target>
<target name="fail">/viewXMLDocument.jsp</target>
<requestparameter get="true" set="false" name="userFile">java.lang.String</requestparameter>
<requestparameter get="true" set="false" name="action">java.lang.String</requestparameter>
<requestparameter get="true" set="false" name="xmlDocument">java.lang.String</requestparameter>
</command>
</project>

[ July 29, 2002: Message edited by: Amy Howard ]
[ July 29, 2002: Message edited by: Amy Howard ]
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Hi Amy
For a given node <xsl:value-of select="."/> will retrieve element value.<xsl:value-of select="name(.)"/> will give you element name.
Regards
Balaji


Spritle Software Blogs
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Some good links:
http://www.zvon.org/xxl/XSLTreference/Output/index.html
http://www.w3.org/TR/xslt
http://www.w3schools.com/xsl/default.asp
Rene
[ July 30, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Amy Howard
Ranch Hand

Joined: Jul 11, 2002
Posts: 86
Thanks for your help. The web links Rene provided are very helpful.
[ July 30, 2002: Message edited by: Amy Howard ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Hm... I tried your XSL with Xalan, didn't get any errors, and it even seems to output what you wanted... How do you apply your stylesheet?
[ July 30, 2002: Message edited by: Mapraputa Is ]
Amy Howard
Ranch Hand

Joined: Jul 11, 2002
Posts: 86
Sorry, I found I had a mistake within the treeTest.html. You are right. The one I posted works.
I don't know why I can't post my code here. Could you please help me with the question I posted in http://www.experts-exchange.com/xml/Q_20330230.html?
Thank you very much for your help!
[ July 30, 2002: Message edited by: Amy Howard ]
[ August 01, 2002: Message edited by: Amy Howard ]
[ August 01, 2002: Message edited by: Amy Howard ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML and XSL