0
votes

i made a .xml about musical content, and i also made a .xsd and i validated the .xml against the .xsd and it worked fine, then i made a .xsl to show the data inside the .xml, it works fine when the .xml has no this line: xmlns="http://www.w3schools.com, it just shows the data before the < xsl:for-each select="catalog/song">:

<?xml version="1.0" encoding="UTF-8"?>
<html>
<body>
  <h2>Billboard Top 5</h2>
  <table border="1">
     <tr>
        <th>Name</th>
        <th>Artist</th>
        <th>Album</th>
        <th>Year</th>
        <th>Genre</th>
        <th>Comments</th>
        <th>Path</th>
     </tr>
  </table>
</body>
</html>

But when I delete this attribute from the catalog tag it works fine:

<?xml version="1.0" encoding="UTF-8"?>
<html>
<body>
  <h2>Billboard Top 5</h2>
  <table border="1">
     <tr>
        <th>Name</th>
        <th>Artist</th>
        <th>Album</th>
        <th>Year</th>
        <th>Genre</th>
        <th>Comments</th>
        <th>Path</th>
     </tr>
     <tr>
        <td>Bed of Roses</td>
        <td>Bon Jovi</td>
        <td>Cross Road</td>
        <td>1995</td>
        <td>rock</td>
        <td>
           <table>
              <tr>
                 <td>joe: it's such a good song</td>
              </tr>
              <tr>
                 <td>maria: really cool</td>
              </tr>
              <tr>
                 <td>mat: fantastic</td>
              </tr>
           </table>
        </td>
        <td>C://music/bon jovi</td>
     </tr>
     <tr>
        <td>Fly Away from Here</td>
        <td>Aerosmith</td>
        <td>Just Push Play</td>
        <td>2001</td>
        <td>rock</td>
        <td>
           <table>
              <tr>
                 <td>elisa: awesome</td>
              </tr>
              <tr>
                 <td>maria: just fine</td>
              </tr>
           </table>
        </td>
        <td>C://music/aerosmith</td>
     </tr>
     <tr>
        <td>Down</td>
        <td>Blink 182</td>
        <td>Blink 182</td>
        <td>2001</td>
        <td>pop</td>
        <td>
           <table>
              <tr>
                 <td>richard: alucinant</td>
              </tr>
              <tr>
                 <td>maria: really fine</td>
              </tr>
           </table>
        </td>
        <td>C://music/blink 182</td>
     </tr>
     <tr>
        <td>Want you bad</td>
        <td>The Offspring</td>
        <td>Conspiracy of One</td>
        <td>2000</td>
        <td>pop</td>
        <td>
           <table>
              <tr>
                 <td>it's old school music</td>
              </tr>
              <tr>
                 <td>maria: love it</td>
              </tr>
           </table>
        </td>
        <td>C://music/the offspring</td>
     </tr>
     <tr>
        <td>The One that you love</td>
        <td>Air Supply</td>
        <td>The One that you love</td>
        <td>1981</td>
        <td>pop</td>
        <td>
           <table>
              <tr>
                 <td>peter: such a classic love song</td>
              </tr>
              <tr>
                 <td>maria: it makes me cry xD</td>
              </tr>
           </table>
        </td>
        <td>C://music/air supply</td>
     </tr>
  </table>
</body>
</html>

i'm using this page to build my xslt : http://www.freeformatter.com/

I think the issue is in the headers of the catalog tab, here my .xml and xsl:

.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
<catalog 
 xmlns="http://www.w3schools.com" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.w3schools.com catalog.xsd">

<song>
 <name2>Bed of Roses</name2>
 <artist>Bon Jovi</artist>
 <album>Cross Road</album>
 <year>1995</year>
 <genre>rock</genre>
 <comments>joe: it's such a good song</comments>
 <comments>maria: really cool</comments>
 <comments>mat: fantastic</comments>
 <path>C://music/bon jovi</path>
</song>
<song>
 <name2>Fly Away from Here</name2>
 <artist>Aerosmith</artist>
 <album>Just Push Play</album>
 <year>2001</year>
 <genre>rock</genre>
 <comments>elisa: awesome</comments>
 <comments>maria: just fine</comments>
 <path>C://music/aerosmith</path>
</song>
<song>
 <name2>Down</name2>
 <artist>Blink 182</artist>
 <album>Blink 182</album>
 <year>2001</year>
 <genre>pop</genre>
 <comments>richard: alucinant</comments>
 <comments>maria: really fine</comments>
 <path>C://music/blink 182</path>
</song>
<song>
 <name2>Want you bad</name2>
 <artist>The Offspring</artist>
 <album>Conspiracy of One</album>
 <year>2000</year>
 <genre>pop</genre>
 <comments>it's old school music</comments>
 <comments>maria: love it</comments>
 <path>C://music/the offspring</path>
</song>
<song>
 <name2>The One that you love</name2>
 <artist>Air Supply</artist>
 <album>The One that you love</album>
 <year>1981</year>
 <genre>pop</genre>
 <comments>peter: such a classic love song</comments>
 <comments>maria: it makes me cry xD</comments>
 <path>C://music/air supply</path>
</song>

</catalog>

.XSD

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

<xs:element name="catalog">
 <xs:complexType>
  <xs:sequence>
    <xs:element name="song" minOccurs="5" maxOccurs="5">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="name2" type="xs:string"/>
          <xs:element name="artist" type="xs:string"/>
          <xs:element name="album" type="xs:string"/>
          <xs:element name="year" type="xs:integer"/>
          <xs:element name="genre" minOccurs="0">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:enumeration value="pop"/>
                <xs:enumeration value="rock"/>
                <xs:enumeration value="jazz"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:element>                     
          <xs:element name="comments" minOccurs="0" maxOccurs="unbounded"
           type="xs:string"/>      
          <xs:element name="path" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>
<xs:unique name="catalog-song-name2-unique">
  <xs:selector xpath="w3:song"/>
  <xs:field xpath="w3:name2"/>
</xs:unique>
</xs:element>
</xs:schema>

.XSL

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<html>
<body>
  <h2>Billboard Top 5</h2>
    <table border="1">
      <tr>
        <th>Name</th>
        <th>Artist</th>
        <th>Album</th>
        <th>Year</th>
        <th>Genre</th>
        <th>Comments</th>
        <th>Path</th>           
      </tr>
      <xsl:for-each select="catalog/song">
      <tr>
        <td><xsl:value-of select="name2"/></td>
        <td><xsl:value-of select="artist"/></td>
        <td><xsl:value-of select="album"/></td>
        <td><xsl:value-of select="year"/></td>
        <td><xsl:value-of select="genre"/></td>
        <td>
          <table>
            <xsl:for-each select="comments">
            <tr>
              <td><xsl:value-of select="."/></td>
            </tr>
            </xsl:for-each>
          </table>
        </td>
        <td><xsl:value-of select="path"/></td>
      </tr>
      </xsl:for-each>
    </table>
 </body>
 </html>  
</xsl:template>
</xsl:stylesheet>

Like I said before, this is an header issue, by header i mean attributes from the main tags. My .xml and .xsd was checked by an expert from this site., so dont worry about them.

1
Search for "XSLT default namespace" and you will find 444 answers to this question. It helps to know the terminology so you know what to search for: I would recommend you do some reading on XML, after that you will know that the thing you call a "header" is actually a namespace declaration.Michael Kay

1 Answers

1
votes

For the XPaths to work, you have to have a namespace prefix on your namespace qualified elements. That's why it works when you remove the default namespace. So either remove the xmlns= from your source document, or prefix your elements and declare (and use) that prefix in your XSLT.

Alternatively, use the local-name function in your XPaths like so:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">    

<html>
<body>
  <h2>Billboard Top 5</h2>
    <table border="1">
      <tr>
        <th>Name</th>
        <th>Artist</th>
        <th>Album</th>
        <th>Year</th>
        <th>Genre</th>
        <th>Comments</th>
        <th>Path</th>           
      </tr>
      <xsl:for-each select="*[local-name()='catalog']/*[local-name()='song']">
      <tr>
        <td><xsl:value-of select="*[local-name()='name2']"/></td>
        <td><xsl:value-of select="*[local-name()='artist']"/></td>
        <td><xsl:value-of select="*[local-name()='album']"/></td>
        <td><xsl:value-of select="*[local-name()='year']"/></td>
        <td><xsl:value-of select="*[local-name()='genre']"/></td>
        <td>
          <table>
            <xsl:for-each select="*[local-name()='comments']">
            <tr>
              <td><xsl:value-of select="."/></td>
            </tr>
            </xsl:for-each>
          </table>
        </td>
        <td><xsl:value-of select="*[local-name()='path']"/></td>
      </tr>
      </xsl:for-each>
    </table>
 </body>
 </html>  
</xsl:template>
</xsl:stylesheet>

Just realize that this method will cause problems if you have elements with the same local-name() in different namespaces. In that case, you could add and namespace-uri()='http://www.w3schools.com' after all the local-name()='...'s and it would take care of that, but that syntax becomes a little cumbersome if you don't have to worry about local name clashes (like in your example document).