Dymond
Dymond

Reputation: 2277

read elements with same name but different attributes

I'm trying to print out the phone numbers of the people in my XML file.

These are the elements that i want to print out

<telefon typ="home">11111111</telefon>
<telefon typ="work">23232323</telefon>
<telefon typ="cell">95959595</telefon>

But I only manage to print out one of them. Of course the first one.

This is my XSL file

 <?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>
<h1>Lista över <xsl:value-of select="count(/*/*/namn)"/> personer</h1><br />
<xsl:for-each select="//person[namn]" >
<h2>Person
<xsl:value-of select="position()" />
<xsl:text>: </xsl:text>
<xsl:value-of select="namn" /></h2>
<h3>Kontaktuppgifter: </h3>
Gata: <xsl:value-of select="adress/gata" />
<br />
Postnr: <xsl:value-of select="adress/postnr" />
<xsl:text> </xsl:text>
<xsl:value-of select="adress/postort" />
<br />
<br />
<xsl:element name="telefon">
<xsl:value-of select="telefon/@typ" /> 
<xsl:text>: </xsl:text> 
<xsl:value-of select="telefon" />
</xsl:element>
<hr />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

If I change the last element to something like this
<xsl:value-of select="." /> It prints out all of the elements, but of course the whole tree structure.

I didn't really wanted to ask here and manage to fix it by my self but after a couple of hours I'm throwing the towel.

Upvotes: 0

Views: 1209

Answers (2)

Borodin
Borodin

Reputation: 126722

You need to add a loop around all of the <telefon> data elements. The code would look something like

<xsl:for-each select="telefon" >
    <xsl:value-of select="@typ" /> 
    <xsl:text>: </xsl:text> 
    <xsl:value-of select="." />
    <br/>
</xsl:for-each>

Upvotes: 2

Mads Hansen
Mads Hansen

Reputation: 66723

It appears that your context (where you are "standing" in the node tree when this piece if XSLT is executing) is the parent of the <telefon> elements.

In your current code, when you use <xsl:value-of select="telefon/@typ"/> the XPath will match all three of the typ attributes, but <xsl:value-of> will return the first one that it encounters.

When you use <xsl:value-of select="."/>, you are getting the calculated string-value of the current node (the parent of the <telefon> elements), which will be the concatenation of all the text() nodes.

You will want something like this:

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

<!--assuming that the document element is the parent element of the telefon elements -->
    <xsl:template match="/*">
        <xsl:apply-templates select="telefon"/>
    </xsl:template>


    <xsl:template match="telefon">
        <!--xsl:copy will do what xsl:element name="telefon" was doing-->
        <xsl:copy>
            <!--select this telefon element's typ attribute value-->
            <xsl:value-of select="@typ"/>
            <xsl:text>: </xsl:text>
            <!--now that the context is the telefon element, value of . will yield expected result -->
            <xsl:value-of select="."/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 3

Related Questions