Xslt to add a default where the item does not exist

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.

+4
source share
2 answers

Try the following:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/> <!-- identity template --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="instance[not(AlbumCount)]"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> <AlbumCount>0</AlbumCount> </xsl:copy> </xsl:template> </xsl:stylesheet> 

Start by transforming your identity, and then just handle the exception differently.

+8
source

You check for the existence of an element simply with the name of the element, for example:

 <xsl:if test="not(AlbumCount)"> <AlbumCount>0</AlbumCount> </xsl:if> 

An easier way to do what you want is to use the standard copy template in combination with a special rule for places where AlbumCount elements need to be added:

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="xml" indent="yes"/> <!-- Standard copy template --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <!-- Special template to add AlbumCount elements where required --> <xsl:template match="records/record/InstanceData/instance"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> <xsl:if test="not(AlbumCount)"> <AlbumCount>0</AlbumCount> </xsl:if> </xsl:copy> </xsl:template> </xsl:stylesheet> 
+3
source

All Articles