Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML and XSL

 
Amy Howard
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This post looks more likely to get answers in our lively and informative XML forum, so I've moved it for you.
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amy Howard
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic