|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 ![]() |
|
#3
|
|||
|
|||
|
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> |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSL Newbie Question. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|