Hey there
Having a bit of a problem, and totally unsure how to work around it.
My XML file looks something like this
<author>
<name>Person A</name>
<book>Book A</book>
</author>
<author>
<name>Person A</name>
<book>Book B</book>
</author>
<author>
<name>Person B</name>
<book>Book Z</book>
</author>
<author>
<name>Person A</name>
<book>Book Y</book>
</book>
I want to merge all books the authors have made into one
ie.
<author>
<name>Person A</name>
<book>Book A</book>
<book>Book B</book>
<book>Book Y</book>
</book>
<author>
<name>Person B</name>
<book>Book Z</book>
</author>
----
My Solution:
I've attempted to try
<xsl:template match="catalogue">
<xsl:element name="author">
<xsl:call-template name="name">
<xsl:with-param name="pos" select="position()"/>
</xsl:call-template>
</xsl:element>
</xsl:template>
<xsl:template name="author">
<xsl
aram name="pos"/>
<xsl:variable name="unique-names"
select="//name[not(.=following::name)]" />
<xsl:for-each select="$unique-names">
<xsl:if test="$pos = position()">
<xsl:element name="name"><xsl:value-of select="."/></xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:template>
-> Which gave me unique list of names
<author>
<name>Author A</name>
</author>
<author>
<name>Author B</name>
</author>
Still unsure of how I would include the books to its corresponding name :S
I'm new to XSLT so any help or suggestions would be of the greatest help!
Having a bit of a problem, and totally unsure how to work around it.
My XML file looks something like this
<author>
<name>Person A</name>
<book>Book A</book>
</author>
<author>
<name>Person A</name>
<book>Book B</book>
</author>
<author>
<name>Person B</name>
<book>Book Z</book>
</author>
<author>
<name>Person A</name>
<book>Book Y</book>
</book>
I want to merge all books the authors have made into one
ie.
<author>
<name>Person A</name>
<book>Book A</book>
<book>Book B</book>
<book>Book Y</book>
</book>
<author>
<name>Person B</name>
<book>Book Z</book>
</author>
----
My Solution:
I've attempted to try
<xsl:template match="catalogue">
<xsl:element name="author">
<xsl:call-template name="name">
<xsl:with-param name="pos" select="position()"/>
</xsl:call-template>
</xsl:element>
</xsl:template>
<xsl:template name="author">
<xsl
<xsl:variable name="unique-names"
select="//name[not(.=following::name)]" />
<xsl:for-each select="$unique-names">
<xsl:if test="$pos = position()">
<xsl:element name="name"><xsl:value-of select="."/></xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:template>
-> Which gave me unique list of names
<author>
<name>Author A</name>
</author>
<author>
<name>Author B</name>
</author>
Still unsure of how I would include the books to its corresponding name :S
I'm new to XSLT so any help or suggestions would be of the greatest help!
Last edited: