I am struggling to check if an element exists. If this is not the case, I would like to add a default value. Here is my XML
<records> <record> <InstanceData> <instance> <FirstName>Johhny</FirstName> <LastName>Jenkins</LastName> <AlbumCount>3</AlbumCount> </instance> </InstanceData> </record> <record> <InstanceData> <instance> <FirstName>Art</FirstName> <LastName>Tatum</LastName> <AlbumCount>7</AlbumCount> </instance> </InstanceData> </record> <record> <InstanceData> <instance> <FirstName>Count</FirstName> <LastName>Basie</LastName> </instance> </InstanceData> </record> </records>
I would like to be able to copy existing values ββand set any record without an Album Count element to <AlbumCount>0</AlbumCount> . This is the xslt that I worked with, but I think I'm a little behind.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="Records"> <xsl:for-each select="node()"> <xsl:choose> <xsl:when test="name()='AlbumCount'"> <xsl:element name="AlbumCount"> <xsl:choose> <xsl:when test="name()='AlbumCount'"> <xsl:copy-of select="."> </xsl:copy-of> </xsl:when> <xsl:otherwise> <AlbumCount>0</AlbumCount> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:when> <xsl:otherwise> <xsl:copy-of select="."> </xsl:copy-of> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Thanks for watching.
Willb source share