codepleb
codepleb

Reputation: 10581

XML + XSL - Doesn't display correct

I read other Questions. The files looked similar, so I don't know, what's the fault here...

Here is my XML:

<?xml version="1.0" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="/Projekt1.xsl"?>
<x:projekte xmlns:x="urn:Projekt1">
    <person id= "1">
        <name>Franz Mommar</name>
        <link>http://pd.zhaw.ch/portraet/images/</link>
        <zhaw>mino.jpg</zhaw>
        <googlemapsx>285.2342</googlemapsx>
        <googlemapsy>234.3598</googlemapsy>
        <facebook>franzmoammar</facebook>
        <skype>franz.moammar</skype>
        <twitter>franzmoammar</twitter>
    </person>
    <person id= "2">
        <name>Rüdiger Mannheim</name>
        <link>http://www.poese.org/wp-content/uploads/2010/07/</link>
        <zhaw>AWNEX74E.jpg</zhaw>
        <googlemapsx>45.2342</googlemapsx>
        <googlemapsy>32.3598</googlemapsy>
        <facebook>franzmoammar</facebook>
        <skype>franz.moammar</skype>
        <twitter>franzmoammar</twitter>
    </person>
</x:projekte>

Here's the xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <tr>
                        <th>Name</th>
                        <th>X-Coordinate</th>
                        <th>Y-Coordinate</th>
                        <th>Facebook</th>
                        <th>Skype</th>
                        <th>Twitter</th>
                        <th>Bild</th>
                    </tr>
                    <xsl:for-each select="projekte/person">
                    <tr>
                        <td><xsl:value-of select="name"/></td>
                        <td><xsl:value-of select="googlemapsx"/></td>
                        <td><xsl:value-of select="googlemapsy"/></td>
                        <td><xsl:value-of select="facebook"/></td>
                        <td><xsl:value-of select="skype"/></td>
                        <td><xsl:value-of select="twitter"/></td>
                        <!--<td><src href="{link}{zhaw}"/></td>-->
                    </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

I'm trying since hours on this files, but I didn't find the solution for this.

I right-click on the XML -> Open with -> Firefox

Upvotes: 2

Views: 346

Answers (2)

Tim C
Tim C

Reputation: 70648

Namespaces could be your problem here. In your XML the projekte element is in the namespace "urn:Projekt1"

<x:projekte xmlns:x="urn:Projekt1">

But there is no reference to the namespace in your XSLT, and so it is looking for a projekte element in no namespace, and so won't match one that is in a namespace.

The solution is to declare the namespace in your XSLT

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
                xmlns:x="urn:Projekt1" exclude-result-prefixes="x">

And then use the prefix in your xpath expression when matching the element

<xsl:for-each select="x:projekte/person">

The "exclude-result-prefixes" is used to stop the output XML including the namespace declaration if no actual elements in the output use the namespace.

Alternatively, you could just use a wild-card in the xpath expression, then you wouldn't have to worry about declaring the namespace

<xsl:for-each select="*/person">

Upvotes: 4

Zeus
Zeus

Reputation: 6586

If you are using '/Project in the source, it assumes to look in the root of the current drive, just use Projekt1.xsl in the source of the xml file, i.e., without the /

Upvotes: 0

Related Questions