aln447
aln447

Reputation: 1011

XSLT transformation fails when XSD added

I have a XML table written, and have made a XSD to validate it with. Now according to this validator (validome, as always not working) the schema validates fine and everything is correct. The problem is the fact that when I implement the xds to the xml...the table simply disappears.

Here is how the XML is implemented + example of table element (Polish)

<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD <!DOCTYPE gry SYSTEM "valid.dtd">  -->


<?xml-stylesheet type="text/xsl" href="gry.xsl"?>
<gry xmlns="http://www.w3schools.com/RedsDevils"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.w3schools.com/RedsDevils schema.xsd">
  <gatunek id="FPS">
    <gra nazwa="Star Wars: Battlefront" PC="y" XBOXONE="n" PS4="n">
      <producent> DICE </producent>
      <wydawca> Electronic Arts </wydawca>
      <cena> 120 </cena>
      <data_wydania>2015-10-10</data_wydania>
      <metacritic>86</metacritic>
      <cover>img/swb.jpg</cover>
            <opis>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</opis>
    </gra> </gatunek> </gry>

And how the xsd goes like:

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

  <xs:element name="gry" >
    <xs:complexType>
      <xs:sequence>
        <xs:element name="gatunek" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="gra" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="producent" type="xs:string"/>
                    <xs:element name="wydawca" type="xs:string"/>
                    <xs:element maxOccurs="1" minOccurs="0" name="cena" type="xs:integer"/>
                    <xs:element maxOccurs="1" minOccurs="0" name="data_wydania" type="xs:date"/>

                    <xs:element maxOccurs="1" minOccurs="0" name="metacritic">
                      <xs:simpleType>
                        <xs:restriction base="xs:byte">
                          <xs:maxInclusive value="100"/>
                          <!-- oceny na metacriticu nie przekraczaja 100 -->
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>

                    <xs:element name="cover" type="xs:anyURI"/>

                    <xs:element name="opis">
                      <xs:simpleType>
                        <xs:restriction base="xs:string">
                          <xs:maxLength value="500"/>
                          <xs:minLength value="20"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>

                    </xs:sequence>

                    <xs:attribute name="nazwa" type="xs:string" use="required"/>
                    <xs:attribute name="PC" use="required">
                      <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:pattern value="(y|n)"/>
                      </xs:restriction>
                    </xs:simpleType>
                    </xs:attribute>

                    <xs:attribute name="XBOXONE" use="required">
                      <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:pattern value="(y|n)"/>
                      </xs:restriction>
                      </xs:simpleType>
                    </xs:attribute>

                    <xs:attribute name="PS4" use="required">
                      <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:pattern value="(y|n)"/>
                      </xs:restriction>
                      </xs:simpleType>
                    </xs:attribute>


              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="id" type="xs:string" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

But not implementing the XSD works fine so:

<?xml-stylesheet type="text/xsl" href="gry.xsl"?>
<gry>
  <gatunek id="FPS">
    <gra nazwa="Star Wars: Battlefront" PC="y" XBOXONE="n" PS4="n">
      <producent> DICE </producent>
      <wydawca> Electronic Arts </wydawca>(....)

Works and displays everything fine. The firebug console shows no errors.

also, here is the xsl file in case it's needed:

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output doctype-system="about:legacy-compat" method="html"/>

  <xsl:template match="/">
    <html>
      <HEAD>
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <link href='https://fonts.googleapis.com/css?family=Caudex:400,700' rel='stylesheet' type='text/css'/>
        <script src="js/jquery.js" type="text/javascript"></script>

        <!--BOOTSTRAP!! -->
        <!-- Latest compiled and minified CSS -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" rel="stylesheet"/>

        <!-- Optional theme -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" rel="stylesheet"/>

        <!-- Latest compiled and minified JavaScript -->
        <script crossorigin="anonymous" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <!-- BOOTSTRAP END -->
        <title>GRY!! :D
        </title>

      </HEAD>
      <BODY>
        <div id="bg"></div>
        <div id="container">
          <h1 id="title">
            GRY 2015 ROKU
          </h1>
          <div id="header">

            <div class="row">
              <div class="col-md-4">
                <div class="menuelm" id="All">
                  <h3>Wszystkie tytuły</h3>
                  <a class="link" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Gat">
                  <h3>Filtruj gatunek:</h3>
                  <select>
                    <option>FPS</option>
                    <option>RPG</option>
                    <option>Strategia</option>
                    <option>Bijatyka</option>
                    <option>Horror</option>
                    <option>Wyscigi</option>
                  </select>
                  <a class="gatlink" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Sys">
                  <h3>Filtruj Konsole:</h3>
                  <div class="row systems">

                    <a href="#pc"><img alt="pc" src="img/sym/pc.jpg"/></a>
                    <a href="#xbo"><img alt="xbox" src="img/sym/xbox.png"/></a>
                    <a href="#ps"><img alt="ps4" src="img/sym/ps4.jpg"/></a>
                  </div>
                </div>
              </div>
            </div>

          </div>
          <!--HEADER END -->

          <div id="tablecover">

            <xsl:call-template name="all"/>
            <script src="js/gry.js" type="text/javascript"></script>
          </div>
        </div>
        <!--container end!-->
        <div style="clear: both"></div>
        <footer>
          <h3>Projekt Alan Krygowski</h3>
        </footer>
      </BODY>
    </html>
  </xsl:template>

  <xsl:template name="all">
    <table class="tablesorter" id="myTable">
      <thead>
        <tr>
          <td>
            <div>
              <p>Nazwa</p>
            </div>
          </td>
          <td>
            <div>
              <p>Gatunek</p>
            </div>
          </td>
          <td>
            <div>
              <p>Producent</p>
            </div>
          </td>
          <td>
            <div>
              <p>Wydawca</p>
            </div>
          </td>
          <td>
            <div>
              <p>Cena</p>
            </div>
          </td>
          <td>
            <div>
              <p>Data Wydania</p>
            </div>
          </td>
          <td>
            <div>
              <p>Metacritic</p>
            </div>
          </td>
          <td>
            <div>
              <p>PC</p>
            </div>
          </td>
          <td>
            <div>
              <p>XBOX</p>
            </div>
          </td>
          <td>
            <div>
              <p>PS4</p>
            </div>
          </td>
        </tr>
      </thead>
      <tbody>

        <!--  <xsl:for-each select="gatunek/gra[../@id='FPS'] | gatunek/gra[../@id='Strategia']">-->

        <xsl:for-each select="gry/gatunek/gra">
          <tr class="{../@id} ">
            <td>
              <div>
                <p><xsl:value-of select="@nazwa"/></p>
              </div>
            </td>
            <td>
              <div>
                <p><xsl:value-of select="../@id"/></p>
              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="producent"/></p>

              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="wydawca"/></p>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(cena)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="cena"/>PLN</p>
                  </xsl:otherwise>
                </xsl:choose>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(data_wydania)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="data_wydania"/></p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(metacritic)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="metacritic"/>/100</p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>

            <!-- OD AUTORA
                Wczesniej, do tych trzech punktow, xsl zawieral zapytanie if odpowiednio
                dostosowujace rodzaj klasy ktora miala byc wstawiona. Zostalo to zamienione
                poniewaz przez dopisywanie klasy zmniejszamy niepotrzebne zablocenie strony -->
            <td>
              <div class="pc {@PC}"></div>
            </td>
            <td>
              <div class="xbo {@XBOXONE}"></div>
            </td>
            <td>
              <div class="ps {@PS4}"></div>
            </td>
          </tr>
          <div class="underbox">
            <img alt="coverart">

              <xsl:attribute name="src">
                <xsl:value-of select="cover"/>
              </xsl:attribute>
            </img>
            <div class="about">
              <p><xsl:value-of select="opis"/></p>
            </div>
          </div>

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

  </xsl:template>

</xsl:stylesheet>

What could be the reason behind this problem, and how can I fix it?

Upvotes: 2

Views: 175

Answers (1)

kjhughes
kjhughes

Reputation: 111491

It's not implementing the XSD that's the problem but rather the introduction of namespace in the XML that's not accounted for in the XSLT.

Add

xmlns:rd="http://www.w3schools.com/RedsDevils"

to the xsl:stylesheet element in your XSLT, and then use the rd namespace prefix where ever you wish to match element of your XML, and your table will work with the XML that includes the xsi:schemaLocation to your XSD.

Fixed XSLT Stylesheet

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rd="http://www.w3schools.com/RedsDevils">

  <xsl:output doctype-system="about:legacy-compat" method="html"/>

  <xsl:template match="/">
    <html>
      <HEAD>
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <link href='https://fonts.googleapis.com/css?family=Caudex:400,700' rel='stylesheet' type='text/css'/>
        <script src="js/jquery.js" type="text/javascript"></script>

        <!--BOOTSTRAP!! -->
        <!-- Latest compiled and minified CSS -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" rel="stylesheet"/>

        <!-- Optional theme -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" rel="stylesheet"/>

        <!-- Latest compiled and minified JavaScript -->
        <script crossorigin="anonymous" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <!-- BOOTSTRAP END -->
        <title>GRY!! :D
        </title>

      </HEAD>
      <BODY>
        <div id="bg"></div>
        <div id="container">
          <h1 id="title">
            GRY 2015 ROKU
          </h1>
          <div id="header">

            <div class="row">
              <div class="col-md-4">
                <div class="menuelm" id="All">
                  <h3>Wszystkie tytuły</h3>
                  <a class="link" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Gat">
                  <h3>Filtruj gatunek:</h3>
                  <select>
                    <option>FPS</option>
                    <option>RPG</option>
                    <option>Strategia</option>
                    <option>Bijatyka</option>
                    <option>Horror</option>
                    <option>Wyscigi</option>
                  </select>
                  <a class="gatlink" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Sys">
                  <h3>Filtruj Konsole:</h3>
                  <div class="row systems">

                    <a href="#pc"><img alt="pc" src="img/sym/pc.jpg"/></a>
                    <a href="#xbo"><img alt="xbox" src="img/sym/xbox.png"/></a>
                    <a href="#ps"><img alt="ps4" src="img/sym/ps4.jpg"/></a>
                  </div>
                </div>
              </div>
            </div>

          </div>
          <!--HEADER END -->

          <div id="tablecover">

            <xsl:call-template name="all"/>
            <script src="js/gry.js" type="text/javascript"></script>
          </div>
        </div>
        <!--container end!-->
        <div style="clear: both"></div>
        <footer>
          <h3>Projekt Alan Krygowski</h3>
        </footer>
      </BODY>
    </html>
  </xsl:template>

  <xsl:template name="all">
    <table class="tablesorter" id="myTable">
      <thead>
        <tr>
          <td>
            <div>
              <p>Nazwa</p>
            </div>
          </td>
          <td>
            <div>
              <p>Gatunek</p>
            </div>
          </td>
          <td>
            <div>
              <p>Producent</p>
            </div>
          </td>
          <td>
            <div>
              <p>Wydawca</p>
            </div>
          </td>
          <td>
            <div>
              <p>Cena</p>
            </div>
          </td>
          <td>
            <div>
              <p>Data Wydania</p>
            </div>
          </td>
          <td>
            <div>
              <p>Metacritic</p>
            </div>
          </td>
          <td>
            <div>
              <p>PC</p>
            </div>
          </td>
          <td>
            <div>
              <p>XBOX</p>
            </div>
          </td>
          <td>
            <div>
              <p>PS4</p>
            </div>
          </td>
        </tr>
      </thead>
      <tbody>

        <!--  <xsl:for-each select="gatunek/gra[../@id='FPS'] | gatunek/gra[../@id='Strategia']">-->

        <xsl:for-each select="rd:gry/rd:gatunek/rd:gra">
          <tr class="{../@id} ">
            <td>
              <div>
                <p><xsl:value-of select="@nazwa"/></p>
              </div>
            </td>
            <td>
              <div>
                <p><xsl:value-of select="../@id"/></p>
              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="rd:producent"/></p>

              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="rd:wydawca"/></p>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(rd:cena)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="rd:cena"/>PLN</p>
                  </xsl:otherwise>
                </xsl:choose>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(rd:data_wydania)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="rd:data_wydania"/></p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(rd:metacritic)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="rd:metacritic"/>/100</p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>

            <!-- OD AUTORA
                Wczesniej, do tych trzech punktow, xsl zawieral zapytanie if odpowiednio
                dostosowujace rodzaj klasy ktora miala byc wstawiona. Zostalo to zamienione
                poniewaz przez dopisywanie klasy zmniejszamy niepotrzebne zablocenie strony -->
            <td>
              <div class="pc {@PC}"></div>
            </td>
            <td>
              <div class="xbo {@XBOXONE}"></div>
            </td>
            <td>
              <div class="ps {@PS4}"></div>
            </td>
          </tr>
          <div class="underbox">
            <img alt="coverart">

              <xsl:attribute name="src">
                <xsl:value-of select="rd:cover"/>
              </xsl:attribute>
            </img>
            <div class="about">
              <p><xsl:value-of select="rd:opis"/></p>
            </div>
          </div>

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

  </xsl:template>

</xsl:stylesheet>

Upvotes: 3

Related Questions