Saturday, 13 February 2016

Filter XML with XSLT

<?xml version="1.0" encoding="utf-8"?>
<teams>
<team><name>Arsenal</name><stadium name="Emirates" capacity="60000" /></team>
<team><name>Crystal Palace</name><stadium name="Selhurst" capacity="26000" /></team>
<team><name>Liverpool</name><stadium name="Anfield" capacity="40000" /></team>
<team><name>Man Utd</name><stadium name="Old Trafford" capacity="70000" /></team>
<team><name>West Ham</name><stadium name="Bolelyn" capacity="30000" /></team>
</teams>
view raw source.xml hosted with ❤ by GitHub

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="teams">
<teams>
<xsl:apply-templates select="team[name='Crystal Palace' or stadium/@capacity > 50000]" />
</teams>
</xsl:template>
<xsl:template match="*">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
<!--
output.indent and strip-space tidy up the xml output
. = self (includes current node)
* = child elements and attributes (excludes current node)
-->
view raw filter.xslt hosted with ❤ by GitHub

<?xml version="1.0" encoding="utf-8"?>
<teams>
<team>
<name>Arsenal</name>
<stadium name="Emirates" capacity="60000" />
</team>
<team>
<name>Crystal Palace</name>
<stadium name="Selhurst" capacity="26000" />
</team>
<team>
<name>Man Utd</name>
<stadium name="Old Trafford" capacity="70000" />
</team>
</teams>
view raw result.xml hosted with ❤ by GitHub

No comments:

Post a Comment