Evan, Thanks for this detailed explanation. Very Helpful! Nate From: [email protected] [mailto:[email protected]] On Behalf Of Evan Lenz Sent: Friday, October 21, 2011 7:41 PM To: General MarkLogic Developer Discussion Subject: Re: [MarkLogic Dev General] xslt empty function
Hi Nate, I can't address what change happened, but the most recent behavior you're seeing is the correct one. fn:empty() only returns true if you give it an empty sequence. See http://www.w3.org/TR/xpath-functions/#func-empty An "empty" (zero-length) string is actually a non-empty sequence of one string (albeit zero-length). If you don't specify the value of $subfields in <xsl:call-template>, then $subfields is initialized with its default value. The default value is determined by how you declare the parameter. Since you used neither a "select" nor "as" attribute (nor a nested sequence constructor), then the default value will be the zero-length string. See http://www.w3.org/TR/xslt20/#element-param (11th para, excluding notes). fn:empty() will work as before if you declare $subfields differently, either by explicitly setting the default value to the empty sequence, like this: <xsl:param name="subfields" select="()"/> Or by providing a type declaration , such as: <xsl:param name="subfields" as="xs:string?"/> Using "as" is probably the better option, since it also effects automatic conversion of supplied values. Since "as" was new in XSLT 2.0, its absence implies the 1.0 behavior (defaulting to empty string rather than empty sequence). Also, instead of $subfields='', I'd recommend using not(string($subfields)) which would work uniformly whether or not $subfields is an empty sequence or an empty string. In contrast, $subfields='' will return true if $subfields is an empty string but false if it's an empty sequence (no zero-length string was found on the left-hand side). Finally, since the rules are so complicated to think about, I'd recommend simplifying things by standardizing on string values. The following declaration will ensure that a value, if supplied, must be a string (or convertible to a string), and if not supplied, will be initialized to the empty string: <xsl:param name="subfields" as="xs:string" select="''"/> Then you can test for empty string with <xsl:if test="not($subfields)">. Evan Lenz Software Developer, Community MarkLogic Corporation [email protected]<mailto:[email protected]> developer.marklogic.com<http://developer.marklogic.com> From: "Trail, Nate" <[email protected]<mailto:[email protected]>> Reply-To: General MarkLogic Developer Discussion <[email protected]<mailto:[email protected]>> Date: Fri, 21 Oct 2011 13:15:48 -0700 To: 'General MarkLogic Developer Discussion' <[email protected]<mailto:[email protected]>> Subject: [MarkLogic Dev General] xslt empty function Hi, We recently upgraded one server from 4.2.1 to 4.2.7, and I noticed a difference between how Marklogic treats "empty" in xslt. I had to change : empty($subfields) to subfields='' In my code, I call a template like this, with our without "subfields": <xsl:call-template name="displayAll"> <xsl:with-param name="label">LC control no.</xsl:with-param> <xsl:with-param name="tag">010</xsl:with-param> <xsl:with-param name="subfields">a</xsl:with-param> </xsl:call-template> or <xsl:call-template name="displayAll"> <xsl:with-param name="label">Main title</xsl:with-param> <xsl:with-param name="tag">245</xsl:with-param> </xsl:call-template> In the template under 4.2.1: <xsl:template name="displayAll"> <xsl:param name="label"/> <xsl:param name="tag"/> <xsl:param name="subfields"/> <xsl:choose> <xsl:when test="empty($subfields)">[Process without subfields]</xsl:when> <xsl:otherwise> [Process with subfields]</xsl:otherwise> </xsl:choose> My new test under 4.2.7 is: <xsl:when test="$subfields='' ">Process without subfields</xsl:when> (Those are two single quotes, not a double quote, after =) Did something change in your xslt processor to affect this? I didn't see it in the upgrade notes. Thanks, Nate --------------------------------------------------------------- Nate Trail --------------------------------------------------------------- Network Development and MARC Standards Office Technology Policy Mail stop 4402 Library Services Library of Congress 202-707-2193 [email protected]<mailto:[email protected]>
_______________________________________________ General mailing list [email protected] http://developer.marklogic.com/mailman/listinfo/general
