for example I have the XML code like this:
<quotes>
<quote>
<character>
<name>LA FEU</name>
</character>
</quote>
<quote>
<character>
<name>LA FEU</name>
</character>
</quote>
<quote>
<character>
<name>LA FEU</name>
</character>
</quote>
</quotes>
My XSLT code to transform this to HTML:
<html>
<body>
<pre>
<xsl:for-each select="quotes/quote">
<!--Output name of the character in underline-->
Name of character: <xsl:apply-templates select="//name"/>
</xsl:for-each>
</pre>
</body>
</html>
Output is like this: Name of character: LAFEULAFEULAFEU
HOw to make the NAME repeat only once every for-each? Would you please help me? THank you
FOr example Name of character: LAFEU
Name of character: LAFEU
Name of character: LAFEU
=================================================================================================
I would like to ask 1 more thing related to this question.
IN case if CHARACTER is under QUOTES many layer, like this:
<quotes>
<quote>
<!--play 1-->
<play>
<playtitle>All's Well That Ends Well</playtitle>
<act>
<acttitle>ACT IV</acttitle>
<scene>
<scenetitle>SCENE I</scenetitle>
<speech>
<name>
<name>LAFEU</name>
</name>
................
HOw to refer to NAME in the For-each statement , I try this, but not work
<xsl:for-each select="quotes/quote">
<!--Output name of the character in underline-->
Name of character: <xsl:value-of select="play/*/name"/>
</xsl:for-each>
Would you please help me with this? THank you
Try this in your XSLT code:
<xsl:for-each select="quotes/quote/character">
<!--Output name of the character in underline-->
Name of character: <xsl:apply-templates select="name"/>
</xsl:for-each>
UPDATE : this solutions is for avoiding doubles on an ordered set, i reread your question, and saw how i probably misinterpret. I leave my answer here, becaust it at least show you how to get rid of an unneeded for-each construction.
The question you do aks however is of a level under beginner i'm afraid. You should learn the basics first imo. Right now you're asking : "do my work for me please".
chekc here first if i may suggest : http://www.w3schools.com/xml/xml_xsl.asp
like this :
NOTE : (almost) never use for-each!!!! certainly not here!!
see here
If you insist on it however, you can use the same preceding-sibling logic if you want in a for-each close.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<pre>
<xsl:apply-templates select="//quote[not(./character/name = preceding-sibling::quote/character/name)]/character/name" />
</pre>
</body>
</html>
</xsl:template>
<xsl:template match="name">
<pre>
<xsl:value-of select="."/>
</pre>
</xsl:template>
Here you go! I have tried to make as few changes to your XSLT as possible to achieve the desired output.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<pre>
<xsl:for-each select="quotes/quote">
<!--Output name of the character in underline-->
Name of character: <xsl:apply-templates select="character/name"/>
</xsl:for-each>
</pre>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
This has the following output :
<html>
<body>
<pre>
Name of character: LA FEU
Name of character: LA FEU
Name of character: LA FEU
</pre>
</body>
</html>
I've just found out good way to deal with this problem: using apply templates, thank you you guys very much :).
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--Handle the document: set up HTML page-->
<xsl:template match="/">
<html>
<body>
<pre>
THIS IS ANOTHER TEST
<!--Display number of quotes in the document-->
There is <xsl:value-of select="count(/*/quote)"/> quotes in the document
<xsl:apply-templates/>
</pre>
</body>
</html>
</xsl:template>
<!--Create the title for the play-->
<xsl:template match="playtitle">
Play title: <xsl:value-of select="."/>
</xsl:template>
<!--Create the title for the scene-->
<xsl:template match="scenetitle">
Scene title: <xsl:value-of select="."/>
</xsl:template>
<!--create the title for act-->
<xsl:template match="acttitle">
Act title: <xsl:value-of select="."/>
</xsl:template>
<!--create the name for the character-->
<xsl:template match="name">
Character name: <span style="font-weight:bold;text-decoration:underline;">
<xsl:value-of select="."/>
</span>
</xsl:template>
<!--create the text for the quote-->
<xsl:template match="line">
Quote text:
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>