XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 22nd, 2004, 07:49 AM
ceriw19 ceriw19 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 ceriw19 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
XSL Newbie Question.

Hi,

Im (very) new to XSL. Im just trying to learn about XSL, XSLT and XPATH after finding XML a pleasure to use.

Im trying to create an XSL file that produces a X/HTML file. The original XML document is an XML version of an exported multiple choice quiz. Here is a sample of the code:


<presentation>
<flow class="Block">
<flow class="QUESTION_BLOCK">
<flow class="FORMATTED_TEXT_BLOCK">
<material>
<mat_extension>
<mat_formattedtext type="SMART_TEXT">Which of the following Java statements contains a syntax error?</mat_formattedtext>
</mat_extension>
</material>
</flow>
<flow class="FILE_BLOCK">
<material/>
</flow>
<flow class="LINK_BLOCK">
<material>
<mattext charset="us-ascii" texttype="text/plain" uri="" xml:space="default"/>
</material>
</flow>
</flow>
<flow class="RESPONSE_BLOCK">
<response_lid ident="response" rcardinality="Single" rtiming="No">
<render_choice maxnumber="0" minnumber="0" shuffle="No">
<flow_label class="Block">
<response_label ident="790ACB00F89E11D687E1687F532E803D" rarea="Ellipse" rrange="Exact" shuffle="Yes">
<flow_mat class="FORMATTED_TEXT_BLOCK">
<material>
<mat_extension>
<mat_formattedtext type="SMART_TEXT">int studentAge;</mat_formattedtext>
</mat_extension>
</material>
</flow_mat>
<flow_mat class="FILE_BLOCK">
<material/>
</flow_mat>
<flow_mat class="LINK_BLOCK">
<material>
<mattext charset="us-ascii" texttype="text/plain" uri="" xml:space="default"/>
</material>
</flow_mat>
</response_label>
</flow_label>
<flow_label class="Block">
<response_label ident="790ACB01F89E11D687E1687F532E803D" rarea="Ellipse" rrange="Exact" shuffle="Yes">
<flow_mat class="FORMATTED_TEXT_BLOCK">
<material>
<mat_extension>
<mat_formattedtext type="SMART_TEXT">studentAge int;</mat_formattedtext>
</mat_extension>
</material>
</flow_mat>
<flow_mat class="FILE_BLOCK">
<material/>
</flow_mat>
<flow_mat class="LINK_BLOCK">
<material>
<mattext charset="us-ascii" texttype="text/plain" uri="" xml:space="default"/>
</material>
</flow_mat>
</response_label>
</flow_label>
<flow_label class="Block">
<response_label ident="790ACB02F89E11D687E1687F532E803D" rarea="Ellipse" rrange="Exact" shuffle="Yes">
<flow_mat class="FORMATTED_TEXT_BLOCK">
<material>
<mat_extension>
<mat_formattedtext type="SMART_TEXT">int studentAge = 21;</mat_formattedtext>
</mat_extension>
</material>
</flow_mat>
<flow_mat class="FILE_BLOCK">
<material/>
</flow_mat>
<flow_mat class="LINK_BLOCK">
<material>
<mattext charset="us-ascii" texttype="text/plain" uri="" xml:space="default"/>
</material>
</flow_mat>
</response_label>
</flow_label>
<flow_label class="Block">
<response_label ident="790ACB03F89E11D687E1687F532E803D" rarea="Ellipse" rrange="Exact" shuffle="Yes">
<flow_mat class="FORMATTED_TEXT_BLOCK">
<material>
<mat_extension>
<mat_formattedtext type="SMART_TEXT">int studentAge; //declare studentAge as an integer</mat_formattedtext>
</mat_extension>
</material>
</flow_mat>
<flow_mat class="FILE_BLOCK">
<material/>
</flow_mat>
<flow_mat class="LINK_BLOCK">
<material>
<mattext charset="us-ascii" texttype="text/plain" uri="" xml:space="default"/>
</material>
</flow_mat>
</response_label>
</flow_label>
</render_choice>
</response_lid>
</flow>
</flow>
</presentation>


That is the source code for a single question!

From this, I now need to display just the question and the four possible answers. Any ideas/suggestions? I have been told that I need to use the position() function...

any help would be much appreciated.

Reply With Quote
  #2  
Old August 22nd, 2004, 09:14 AM
kid23 kid23 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 62 kid23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
same answer here as in my previous post.. can't give u an answer without actually doing it 100% for you, which isn't going to help you in the longer term.

No offense, but you should spend some time on xsl/xslt tutorials, for instance those on w3schools. They're easy to read, don't take long, and if you find xml easy to use, you'll easily gain experience on xsl/xslt.

In a few words, the best thing I can explain you is that xsl works with templates, which are user defined, and consists in rules that you define to process your xml tags.

A quick example, if in XML you have a tag called <AnyTag>, you're going to write a it's template into xsl like this :

<xsl:template match="AnyTag">
... any html code in here
</xsl:template>

This is the most important thing to understand, all the rest comes naturally afterwards

ow, and one last word.. you "might" have to use position() in your solution.. but this is only a function, and it's far from being the answer to all the problems you'll face while writing your code

Reply With Quote
  #3  
Old August 27th, 2004, 05:53 AM
ceriw19 ceriw19 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 ceriw19 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Well, after some reading and looking at the w3schools.com website, this is what Ive come up with, but it still dont work! Any ideas where Im going wrong?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<body>
<h2>Quiz 1</h2>
<script type="text/vbscript">

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("pool1source.xml")

path="/questestinterop/assesment/section/item/presentation/flow/flow/flow/material/mat_extension/mat_formattedtext[type=SMART_TEXT]"
set nodes=xmlDoc.selectNodes(path)

for each x in nodes
document.write("<xmp>")
document.write(x.xml)
document.write("</xmp>")
next

</script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Reply With Quote
  #4  
Old August 27th, 2004, 03:16 PM
kid23 kid23 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 62 kid23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
If you're only looking for the question and the 4 possible answers, then this will be enough:

xsl:
Code:
<xsl:template match="presentation">
  <html>
    <body>
      <xsl:apply-templates select="//mat_formattedtext" />
    </body>
  </html>
</xsl:template>

<xsl:template match="mat_formattedtext">
  <xsl:value-of select="text()" />
  <br />
</xsl:template>


explanation: I've created two templates, the first one takes care of the root element in xml (presentation) and creates the basic structure of an html document.
In the middle of it, I ask xslt to apply the templates for all the nodes named mat_formattedtext, no matter where they are in the document (this is done using the //)

The second template is used to process the selected nodes, everytime a mat_formattedtext is encountered, it's text content will be displayed, and followed by an html BR.

You can of course enhance your page at will..

Grtz

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL Newbie Question.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT