Skip to content

Instantly share code, notes, and snippets.

@greystate
Last active December 15, 2015 04:49
Show Gist options
  • Save greystate/5203973 to your computer and use it in GitHub Desktop.
Save greystate/5203973 to your computer and use it in GitHub Desktop.
Demo of how to grab all `<affiliate>` nodes with the `@refID` as specified by the `$refID` variable – *or*, if the $refID variable is empty, take all `<affiliate>`s ...
<root>
<affiliate refID="1200">1200</affiliate>
<affiliate refID="1201">1201</affiliate>
<affiliate refID="1202">1202</affiliate>
<affiliate refID="1203">1203</affiliate>
<affiliate refID="1204">1204</affiliate>
<affiliate refID="1205">1205</affiliate>
<affiliate refID="1206">1206</affiliate>
<affiliate refID="1207">1207</affiliate>
<affiliate refID="1208">1208</affiliate>
</root>
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:variable name="refID" select="1202" />
<xsl:variable name="affiliates" select="/root/affiliate" />
<xsl:template match="root">
<result>
<xsl:apply-templates select="
$affiliates[normalize-space($refID)][@refID = $refID]
|
$affiliates[not(normalize-space($refID))]
" />
</result>
</xsl:template>
<xsl:template match="affiliate">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>
@greystate
Copy link
Author

—To test the empty value for $refID, either use "/.." (guaranteed to always be an empty set) or '' :

<xsl:variable name="refID" select="/.." />

or:

<xsl:variable name="refID" select="''" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment