I'm new to XSLT -- I'm trying to utilize this to recreate (copy) an existing xml document based on a specified attribute.
As an example, with below XML, I want to get the lesson that has the attribute tags="6" from the mainGroup that has the attribute of group="Book1"
<Groups>
<mainGroup id="1" group="Book1">
<subGroup name="Chapter 1">
<lesson name="Lesson1" tags="1" />
<lesson name="Lesson2" tags="2" />
</subGroup>
<subGroup name="Chapter 2">
<lesson name="Lesson1" tags="3" />
<lesson name="Lesson2" tags="4" />
</subGroup>
<subGroup name="Chapter 3">
<subGroup name="Chapter 3 Examples">
<lesson name="Lesson2" tags="5" />
</subGroup>
<lesson name="Lesson1" tags="6" />
</subGroup>
</mainGroup>
<mainGroup id="1" group="Book2">
<subGroup name="Chapter 1">
<lesson name="Lesson1" tags="1" />
<lesson name="Lesson2" tags="2" />
</subGroup>
<subGroup name="Chapter 2">
<lesson name="Lesson1" tags="3" />
<lesson name="Lesson2" tags="4" />
</subGroup>
<subGroup name="Chapter 3">
<subGroup name="Chapter 3 Examples">
<lesson name="Lesson2" tags="6" />
</subGroup>
<lesson name="Lesson1" tags="5" />
</subGroup>
</mainGroup>
</Groups>
with an expected (anticipated) result of the below after XSL is applied
<Groups>
<mainGroup id="1" group="Book1">
<subGroup name="Chapter 3">
<lesson name="Lesson1" tags="6" />
</subGroup>
</mainGroup>
</Groups>
As far as the XSL, what I've come up with so far and the closest I've gotten is:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
// Remove all maingroups that are not book1
<xsl:template match="mainGroup[not(@group='Book1')]" />
// Remove all lessons that do not have tags of 5
<xsl:template match="lesson[not(@tags='5')]" />
</xsl:stylesheet>
which gives me the XML result of:
<Groups>
<mainGroup id="1" group="Book1">
<subGroup name="Chapter 1" />
<subGroup name="Chapter 2" />
<subGroup name="Chapter 3">
<subGroup name="Chapter 3 Examples">
<lesson name="Lesson2" tags="5" />
</subGroup>
</subGroup>
</mainGroup>
</Groups>
I cannot figure out how to now remove the empty subGroups --
Some things I've tried (again please don't laugh - new at this XSL :) )
Hoping this removed those that didn't have a lesson node - but didn't seem to do anything
<xsl:template match="subGroup[count(lesson) = 0]" />
<xsl:template match="subGroup[not(node())]" />
and even something like this ---
<xsl:template match="subGroups" mode="copy">
<xsl:choose>
<xsl:when test="count(./*)">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="copy"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
but after hours of no success I'm throwing in the towel - I'm sure someone here probably reading this is thinking -- ahhh, you just need to add or do this -- so any help would be appreciated.
Thanks