kakka47
kakka47

Reputation: 3509

output from external XML file

I created a xsl file that will output two XML:s.

One of the xml should be reaped just once. since its only one structure and its working so I wont post that code, but the in the other I want to output the whole tree structure. But its only prints the first one. and not the entire tree.

This is what Ill come up to at the moment

<?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>


<xsl:for-each select="$doc1//quiz//question">   
<xsl:value-of select="$doc1//author" />
<br />
<xsl:value-of select="$doc1//questionText" />
<br />
<xsl:text>Ger </xsl:text><xsl:value-of select="$doc1//points" /><xsl:text> Poäng </xsl:text>
</xsl:for-each>

</body> 
</html> 
</xsl:template> 
</xsl:stylesheet>

thanks

Edit: Will ad my XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="q2.xsl"?>
<quiz>
<question>
<author>Författare 1</author>
<questionText>Fråga 1</questionText>
<correct>Svar 1</correct>
<incorrect>fel 1</incorrect>
<incorrect>fel 1-2</incorrect>
<points>1</points>
</question>

<question>
<author>Författare 2</author>
<questionText>Fråga 2</questionText>
<correct>Svar 2</correct>
<incorrect>fel 2</incorrect>
<incorrect>fel 2-3</incorrect>
<points>2</points>
</question>

<question>
<author>Författare 3</author>
<questionText>Fråga 3</questionText>
<correct>Svar 3</correct>
<incorrect>fel 3</incorrect>
<incorrect>fel 3-4</incorrect>
<points>3</points>
</question>

<question>
<author>Författare 4</author>
<questionText>Fråga 4</questionText>
<correct>Svar 4</correct>
<incorrect>fel 4</incorrect>
<incorrect>fel 4-5</incorrect>
<points>4</points>
</question>

<question>
<author>Författare 5</author>+
<questionText>Fråga 5</questionText>
<correct>Svar 5</correct>
<incorrect>fel 5</incorrect>
<incorrect>fel 5-6</incorrect>
<points>5</points>
</question>

</quiz>

Upvotes: 0

Views: 124

Answers (1)

Treemonkey
Treemonkey

Reputation: 2163

Your problem is you reference back to the origional $doc instead of the actual context node when you enter the for-each statement

This should work

<?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>


<xsl:for-each select="$doc1/quiz/question">   
<xsl:value-of select="author" />
<br />
<xsl:value-of select="questionText" />
<br />
<xsl:text>Ger </xsl:text><xsl:value-of select="points" /><xsl:text> Poäng </xsl:text>
</xsl:for-each>

</body> 
</html> 
</xsl:template> 
</xsl:stylesheet>

Extra example using apply templates!

This is much better for reading ect. once things get complicated, and you can easily note the context of nodeset!

<?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>
                <xsl:apply-templates select="$doc1/quiz/question"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="question">
        <xsl:value-of select="author" />
        <br />
        <xsl:value-of select="questionText" />
        <br />
        <xsl:text>Ger </xsl:text>
        <xsl:value-of select="points" />
        <xsl:text> Poäng </xsl:text>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions