pexxx

Hi,

I have an xml doc like this (sample):


<Q id="10">

this is a question

<A nextq="20">

this is a possible answer

</A>

<A nextq="30">

this is an other possible answer

</A>

</Q>

<Q id="20">

this is a question

<A>

this is a possible answer

</A>

<A nextq="30">

this is an other possible answer

</A>

</Q>

<Q id="30">

this is a question

<A>

this is a possible answer

</A>

<A >

this is an other possible answer

</A>

</Q>

I'm using a parameterized xsl:template that is called whith a start question id as it's argument. Questions indicated in the XML with the "nextq" attribute should also appear in the output. The question is how to prevent duplicate questions in the output.

In the sample above 2 answers refer to Question id 30, but in the output this question should appeare once.

Sample output:

<div id="10">

this is a question

<br>

<a href="blablabla"> this is a possible answer</a>

<br>

<a href="blablabla"> this is an other possible answer</a>

</div>

<div id="20">

this is a question

<br>

<a href="blablabla"> this is a possible answer</a>

<br>

<a href="blablabla"> this is an other possible answer</a>

</div>

<div id="30">

this is a question

<br>

<a href="blablabla"> this is a possible answer</a>

<br>

<a href="blablabla"> this is an other possible answer</a>

</div>

How do i keep track of the already exported questions



Re: XML and the .NET Framework How to avoid duplicate output using xslt (More help needed)

Martin Honnen

If I understand your problem description correctly then you can define a key on the nextq attribute of the A elements and simply make sure you process the first A element having a certain nextq value. That way it becomes a standard grouping problem so it can be solved like this:

Code Snippet

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

<xsl:param name="start-id" select="10"/>

<xsl:output method="html" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:key name="unique-next" match="A" use="@nextq"/>

<xsl:template match="/">
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<xsl:apply-templates select="Questions/Q[@id = $start-id]"/>
</body>
</html>
</xsl:template>

<xsl:template match="Q">
<div id="{@id}">
<xsl:apply-templates/>
</div>
<xsl:apply-templates select="A[generate-id() = generate-id(key('unique-next', @nextq)[1])]" mode="next-question"/>
</xsl:template>

<xsl:template match="A" mode="next-question">
<xsl:apply-templates select="../../Q[@id = current()/@nextq]"/>
</xsl:template>

<xsl:template match="A">
<br/>
<a href="#"><xsl:apply-templates/></a>
</xsl:template>

</xsl:stylesheet>






Re: XML and the .NET Framework How to avoid duplicate output using xslt (More help needed)

pexxx

I will try to fit your sample in. I will let you know if it works. (will take some time, am a few days out of the office)

Thanks for the reply

Perry





Re: XML and the .NET Framework How to avoid duplicate output using xslt (More help needed)

pexxx

Thanks Martin, this did the trick!



Re: XML and the .NET Framework How to avoid duplicate output using xslt (More help needed)

pexxx

I have still got a problem (see xml data and xslt template)

XML Data:

Code Snippet

<Questions>

<Question>

<Q id="10">

this is question 10

<A nextq="20">this is a possible answer to q20</A>

<A nextq="20">this is a possible answer to q20</A>

<A nextq="30">this is an other possible answer to q30</A>

<A nextq="20">this is a possible answer to q20</A>

</Q>

</Question>

<Question>

<Q id="20">

this is question 20

<A>this is a possible answer to nothing</A>

<A>this is an other possible answer to nothing</A>

</Q>

</Question>

<Question>

<Q id="30">

this is question 30

<A>this is a possible answer to nothing</A>

<A>this is an other possible answer to nothing</A>

<A nextq="20">this is an other possible answer to q20</A>

</Q>

</Question>

</Questions>

Xslt sheet:

Code Snippet

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

<xsl:param name="start-id" select="10"/>

<xsl:output method="html" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:key name="unique-next" match="A" use="@nextq"/>

<xsl:template match="/">

<html lang="en">

<head>

<title>Example</title>

</head>

<body>

<xsl:apply-templates select="//Q[@id = $start-id]"/>

</body>

</html>

</xsl:template>

<xsl:template match="Q">

<br/>

<div id="{@id}">

<xsl:value-of select="./text()"/>

<xsl:apply-templates select="A"/>

</div>

<xsl:apply-templates select="A[generate-id() = generate-id(key('unique-next', @nextq)[1])]" mode="next-question"/>

</xsl:template>

<xsl:template match="A" mode="next-question">

<xsl:apply-templates select="//Q[@id = current()/@nextq]"/>

</xsl:template>

<xsl:template match="A">

<br/>

<a href="#">

<xsl:apply-templates/>

</a>

</xsl:template>

</xsl:stylesheet>

If i run this sheet, with $start-id = 10, everything is ok, but when i run this using $start-id = 30, question id 20 is not exported.

Anyone an idea