I am trying to filter a record set, and i have got it to work with the following XSLT, but it doesnt look nice at all:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:r="http://www.castiron.com/response" exclude-result-prefixes="r">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Response">
<xsl:element name="rcode">
<xsl:text>0</xsl:text>
</xsl:element>
<xsl:element name="rmesage">
<xsl:text>0</xsl:text>
</xsl:element>
<xsl:element name="payload">
<xsl:for-each select="Response/payload/globalBuy[season='A09']">
<xsl:element name="season"><xsl:value-of select="season"/></xsl:element>
<xsl:element name="productId"><xsl:value-of select="productId"/></xsl:element>
<xsl:element name="globalBuyFlag"><xsl:value-of select="globalBuyFlag"/></xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
In addition i am seeing issues when a namespace pops up in the root. Sample XML below:
<?xml version="1.0" encoding="UTF8"?>
<Response xmlns="http://www.castiron.com/response">
<payload>
<globalBuy>
<season>CD12</season>
<productId>123456</productId>
<globalBuyFlag>XXL</globalBuyFlag>
</globalBuy>
</payload>
</Response>
You could certain simplify your code by removing all the uses of xsl:element. Unless you want the element name to be dynamically generated, it is much simple to just write out the element as normal XML. So instead of writing this….
Just write this…
As for filtering, if all you want to do is copy the XML, but only including certain items that match the filter, then you could override the identity template, and have templates matching the items you don’t want to include, and simply ignore them.
Try this simplified XSLT
When applied to the following XML
Then the following is output
Do note I removed all namespaces in this example to keep things simpler.
EDIT: If you do want to handle a default namespace, in XSLT 1.0 you would have explicity refer to the namespaces for each element you are matching. You would do something like this: