schaeffer.heng
schaeffer.heng

Reputation: 25

convert xml catalog to html with xslt

My goal is to convert xml catalogs to html. The xml schema and the xml file is well formed and valid.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com"
    xmlns="http://www.w3schools.com"
    elementFormDefault="qualified">


<xs:element name="catalog"/>

<xs:complexType name="textint">
    <xs:sequence>
        <xs:element name="s" type="xs:string"/>
        <xs:element name="i" type="xs:int"/>
        <xs:element name="s" type="xs:string"/>
    </xs:sequence>
</xs:complexType>


<xs:complexType mixed="true" name="inttext">
    <xs:sequence>
        <xs:element name="i" type="xs:int"/>
        <xs:element name="s" type="xs:string"/>
        <xs:element name="i" type="xs:int"/>
        <xs:element name="s" type="xs:string"/>
    </xs:sequence>
</xs:complexType>




<xs:element name="Qstr">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="text" type="xs:string"/>
            <xs:element name="a" type="xs:string"/>
            <xs:element name="b" type="xs:string"/>
            <xs:element name="c" type="xs:string"/>
            <xs:element name="d" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>





<xs:element name="Qfl">
    <xs:complexType mixed="true">
        <xs:sequence>
            <xs:element name="text" type="textint"/>
            <xs:element name="a" type="xs:int"/>
            <xs:element name="b" type="xs:decimal"/>
            <xs:element name="c" type="xs:int"/>
            <xs:element name="d" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>




<xs:element name="Qinttext">
    <xs:complexType mixed="true">
        <xs:sequence>
            <xs:element name="text" type="inttext"/>
            <xs:element name="a" type="xs:int"/>
            <xs:element name="b" type="xs:int"/>
            <xs:element name="c" type="xs:int"/>
            <xs:element name="d" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>




<xs:element name="Qtextint">
    <xs:complexType mixed="true">
        <xs:sequence>   
            <xs:element name="text" type="xs:string"/>
            <xs:element name="a" type="textint"/>
            <xs:element name="b" type="xs:string"/>
            <xs:element name="c" type="xs:string"/>
            <xs:element name="d" type="textint"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>



</xs:schema>

.

<?xml version="1.0" encoding="UTF-8"?>

<catalog xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com   file:///home/n/workspace/webprog1/WebContent/schema.xml">

    <Qstr>
        <text>Welcher Mechanismus kann unter Unix zur Kommunikation über das Netzwerk verwendet werden?</text>
        <a>Sockets</a>
        <b>Message Queues</b>
        <c>Pipes</c>
        <d>Semaphore</d>
    </Qstr>

    <Qstr>
        <text>Die Hauptstadt von Italien ist:</text>
        <a>Rom</a>
        <b>Athen</b>
        <c>Bonn</c>
        <d>Madrid</d>
    </Qstr>

    <Qfl>
        <text> <s>Die Quadratwurzel von </s> <i>100</i> <s> ist:</s></text>
        <a>10</a>
        <b>2.76</b>
        <c>5</c>
        <d>1</d>
    </Qfl>

    <Qinttext>
        <text> <i>1</i> <s>+</s> <i>1</i> <s>= ?</s> </text>
        <a>2</a>
        <b>1</b>
        <c>3</c>
        <d>4</d>
    </Qinttext>

    <Qtextint>
        <text>Spinnen...</text>
        <a> <s>...haben </s> <i>8</i> <s> Beine</s> </a>
        <b>...sind Insekten</b>
        <c>...sind Vögel</c>
        <d> <s>...werden bis zu </s> <i>100</i> <s> Jahre alt</s> </d>
    </Qtextint>

    <Qstr>
        <text>Die Hauptstadt von Spanien ist:</text>
        <a>Madrid</a>
        <b>Barcelona</b>
        <c>Rom</c>
        <d>London</d>
    </Qstr>

</catalog>

Next I created an xsl stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />

  <xsl:template match="/">

<html>

  <head>

   <title>Simple Quiz</title>

  </head>

  <body>

<xsl:for-each select="catalog/Qstr">

<table border="1">
    <tr bgcolor="orange">
      <th>Frage</th>
    </tr>
    <tr>
      <td><xsl:value-of select="text"/></td>
    </tr>
    <tr bgcolor="orange">
      <th>Antwortmöglichkeiten</th>
    </tr>
    <tr>
      <td><xsl:value-of select="a"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="b"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="c"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="d"/></td>
    </tr>


  </table>
</xsl:for-each>


<xsl:for-each select="catalog/Qfl">

<table border="1">
    <tr bgcolor="orange">
      <th>Frage</th>
    </tr>
    <tr>
      <td><xsl:value-of select="text"/></td>
    </tr>
    <tr bgcolor="orange">
      <th>Antwortmöglichkeiten</th>
    </tr>
    <tr>
      <td><xsl:value-of select="a"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="b"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="c"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="d"/></td>
    </tr>


  </table>
</xsl:for-each>


<xsl:for-each select="catalog/Qinttext">

<table border="1">
    <tr bgcolor="orange">
      <th>Frage</th>
    </tr>
    <tr>
      <td><xsl:value-of select="text"/></td>
    </tr>
    <tr bgcolor="orange">
      <th>Antwortmöglichkeiten</th>
    </tr>
    <tr>
      <td><xsl:value-of select="a"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="b"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="c"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="d"/></td>
    </tr>


  </table>
</xsl:for-each>


<xsl:for-each select="catalog/Qtextint">

<table border="1">
    <tr bgcolor="orange">
      <th>Frage</th>
    </tr>
    <tr>
      <td><xsl:value-of select="text"/></td>
    </tr>
    <tr bgcolor="orange">
      <th>Antwortmöglichkeiten</th>
    </tr>
    <tr>
      <td><xsl:value-of select="a"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="b"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="c"/></td>
    </tr>
    <tr>
      <td><xsl:value-of select="d"/></td>
    </tr>


  </table>
</xsl:for-each>

  </body>

 </html>

  </xsl:template>

</xsl:stylesheet>

As I am using ubuntu I chose oxygen for xml editing. The transformation was succesful, but nothing is displayed in my html. I guess that my problem has to do with the xml schema. After using the solution of Rad Lexus the problem still remains.

Upvotes: 1

Views: 91

Answers (1)

Jongware
Jongware

Reputation: 22457

Your line <xsl:for-each select="catalog/Qstr"> already points the active path to /catalog/Qstr/. Therefore, you must not repeat it inside the for-each as .. select="catalog/Qstr/a" – the accumulated path then becomes

/catalog/Qstr/catalog/Qstr/a

Using this is sufficient:

<td><xsl:value-of select="text" /></td>

(and the same for your other value-ofs).

Upvotes: 3

Related Questions