Reputation: 443
I know that questions similar to this have been asked but I can't work out what is wrong with my test.
Here are 3 variations of how my XML could be like;
I am trying to work out the presence of a value for <apsite:address>
, an empty string or a missing element.
<apsite:apsite xmlns="http://www.w3.org/1999/xhtml" >
<apsite:name>John</apsite:name>
<apsite:address>Some Address</apsite:name>
</apsite:apsite>
<apsite:apsite xmlns="http://www.w3.org/1999/xhtml" >
<apsite:name>John</apsite:name>
<apsite:address></apsite:name>
</apsite:apsite>
<apsite:apsite xmlns="http://www.w3.org/1999/xhtml" >
<apsite:name>John</apsite:name>
</apsite:apsite>
I need to find the instances where the value of is an empty string or null. These are my if conditions.
<xsl:if
test="../apsite:address = ''">
<dc:source>
<xsl:value-of select="." />
</dc:source>
</xsl:if>
<xsl:if
test="not(apsite:apsite/apsite:address)">
<dc:source>
<xsl:value-of select="." />
</dc:source>
</xsl:if>
<xsl:if
test="../apsite:adress != ''">
<dc:source>
<xsl:value-of select="../apsite:oldAddress" />
</dc:source>
</xsl:if>
My test for the empty string works but not for the missing element i.e test="not(apsite:apsite/apsite:address)"> is not working.
Could someone please advise what I am missing ?
Thanks.
Upvotes: 0
Views: 2487
Reputation: 12729
This XSLT 1.0 stylesheet ...
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:apsite="http://stackoverflow.com/questions/18524099"
xmlns:dc="some-other-url" >
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="apsite:address[.='']">
<dc:source>
<xsl:value-of select="'New value'" />
</dc:source>
<xsl:comment>'address' element was empty.</xsl:comment>
</xsl:template>
<xsl:template match="apsite:address[not(.='')]">
<dc:source>
<xsl:value-of select="." />
</dc:source>
<xsl:comment>Text of 'address' element was copied.</xsl:comment>
</xsl:template>
<xsl:template match="apsite:apsite[not(apsite:address)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<dc:source>
<xsl:value-of select="'New value'" />
</dc:source>
</xsl:copy>
<xsl:comment>'address' element was absent.</xsl:comment>
</xsl:template>
</xsl:stylesheet>
...when applied in turn to each of these 3 possible input documents...
Test case 1: Input document:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
<apsite:address>Some Address</apsite:address>
</apsite:apsite>
Test case 2: Input document:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
<apsite:address></apsite:address>
</apsite:apsite>
Test case 3: Input document:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
</apsite:apsite>
...yields respectively...
Test Case 1 output:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
<dc:source xmlns:dc="some-other-url">Some Address</dc:source>
<!--Text of 'address' element was copied.-->
</apsite:apsite>
Test Case 2 output:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
<dc:source xmlns:dc="some-other-url">New value</dc:source>
<!--'address' element was empty.-->
</apsite:apsite>
Test Case 3 output:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
<dc:source xmlns:dc="some-other-url">New value</dc:source>
</apsite:apsite>
<!--'address' element was absent.-->
I hope this helps.
Upvotes: 1