RE: [xsl] Forgive the noob

Subject: RE: [xsl] Forgive the noob
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 8 Aug 2008 22:24:37 +0100
> Could you show me what that if ($buyer) then 
> fn:borrowerfullname($buyer) else () would look like in the 
> function?  Is this the 'right' way?
> 
> <xsl:choose>
> 	<xsl:when test="$buyer">
> 		...
> 	</xsl:when>
> </xsl:choose>

That's one way. Another way (if there's no "otherwise" branch) is

<xsl:if test="$buyer">
   ...
</xsl:if>

or you can do the logic at the XPath level:

<xsl:function ...>
  <xsl:param name="buyer" as="element(buyer)"/>
  <xsl:sequence select="if ($buyer) then ... else ()"/>
</xsl:function>

> 
> How do you return an empty string or empty element() set?

You can return an empty string the same way as any other string, for example

<xsl:sequence select="''"/>

You can return an empty sequence as 

<xsl:sequence select="()"/>

or just by not returning anything, as in the <xsl:if> example.

Incidentally, the concept of "an empty element() set" is meaningless in the
XPath model. An empty sequence is just an empty sequence; there's no
difference between an empty sequence of apples and an empty sequence of
oranges.

Michael Kay
http://www.saxonica.com/


> 
> Chris Bordeman
> Senior Software Developer
> AppOne, Inc., a Wolters Kluwer Financial Services Company 
> "Connecting dealers & lenders"
> http://www.appone.net
> http://www.dmsone.net
> 225. 754. 5912 (P)
> 866. 422. 9910 (F)
>  
> 
> -----Original Message-----
> From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
> Sent: Friday, August 08, 2008 3:43 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] Forgive the noob
> 
> There's no "up front" in XSLT: no after, no before, no 
> concept of time at all. It's a stateless language. 
> Concepually, everything happens at once.
> Accordingly, there's no concept of writing to a variable "at 
> the start"
> and reading it "later".
> 
> Your two <xsl:variable name="buyerfullname"/> instructions 
> are declaring completely unrelated variables, the only thing 
> they have in common is that they both have the same name.
> 
> Just do the initialisation within the declaration of the global
> variable:
> 
> <xsl:variable name="buyer"
> > select="Contract/Borrowers/Borrower[NumOrder='1']"/>
> 
> <xsl:variable name="buyerfullname"
>   select="fn:borrowerfullname($buyer)"/>
> 
> You could make that
> 
> <xsl:variable name="buyerfullname"
>   select="if ($buyer) then fn:borrowerfullname($buyer) else ()"/>
> 
> but I would personally put the logic for that inside the function.
> 
> Michael Kay
> http://www.saxonica.com/
> 
> > -----Original Message-----
> > From: Bordeman, Chris [mailto:Chris.Bordeman@xxxxxxxxxxxxxxxxx]
> > Sent: 08 August 2008 21:00
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > Subject: [xsl] Forgive the noob
> > 
> > Hi guys, doing my first XSLT and need a bit of help.
> > 
> > Basically, I'm transforming essentially a serialized object 
> structure 
> > into a list of field-value pair nodes.
> > 
> > There are a lot of complicated nodes I need to get at 
> often, like the 
> > first address node under the first customer so I wanted to set up a 
> > bunch of variables and locate and and assign helper 
> variables before I
> 
> > get into the template proper, which is itself very simple.  I've 
> > written some functions to help w/ this and a single named template 
> > called "init"
> > I call up front to set up the variables.
> > 
> > The problem is when I assign to the variables inside my named 
> > template, they don't keep their value.  I'd just add this 
> stuff to the
> 
> > main template but I really need to split all this init 
> stuff into an 
> > include file or something so they can be reused in other xslts.
> > 
> > How can I make these variables visible where I need to use them?  
> > Here's a bit of what I'm doing now:
> > 
> > 	<!-- declare global helper variables -->
> > 	<xsl:variable name="buyerfullname" select="''"/>
> > 	<xsl:variable name="buyeraddress" select="''"/>
> > 	<xsl:variable name="buyeraddress1" select="''"/>
> > 	<xsl:variable name="buyercsz" select="''"/>
> > 	<xsl:variable name="buyerhomephone" select="''"/>
> > 	<xsl:variable name="buyerblock" select="''"/>
> > 
> > 	<xsl:template name="init">
> > 		<xsl:variable name="buyer"
> > select="Contract/Borrowers/Borrower[NumOrder='1']"/>
> > 		<xsl:choose>
> > 			<xsl:when test="string($buyer) != ''">
> > 				<xsl:variable name="buyerfullname"
> > select="fn:borrowerfullname($buyer)"/>
> > 				<xsl:variable name="buyeraddress"
> > select="$buyer/Addresses/BorrowerAddress[IsCurrent='true' and 
> > position()=1]"/>
> > 				<xsl:variable name="buyeraddress1"
> > select="fn:address1($buyeraddress)"/>
> > 				<xsl:variable name="buyercsz"
> > select="fn:csz($buyeraddress)"/>
> > 				<xsl:variable name="buyerhomephone"
> > select="$buyer/HomePhone/PhoneNumber"/>
> > 				<xsl:variable name="buyerblock"
> > select="string-join(($buyerfullname,$buyeraddress1,$buyercsz,$
> buyerhomep
> > hone),fn:crlf())"/>
> > 			</xsl:when>
> > 		</xsl:choose>
> > 	</xsl:template>
> > 
> > 	<!-- create helper variables -->
> > 	<xsl:call-template name="init"/>
> > 
> > 	<!-- Main Template, generates FieldValuePairs -->
> > 	<!-- ERRORS: I GET 'VARIABLE DOES NOT EXIST' ERRORS -->
> > 	<xsl:template match="/">	
> > 		<FieldValuePairs>
> > 			<FieldValuePair field="BuyerState">
> > ERROR --->			<xsl:attribute
> > name="value"><xsl:value-of
> > select="$buyeraddress/State"/></xsl:attribute>
> > 			</FieldValuePair>
> > 			<FieldValuePair
> > field="BuyerFullNameStreetCityStateZipPhone">
> > ERROR --->			<xsl:attribute
> > name="value"><xsl:value-of select="$buyerblock"/></xsl:attribute>
> > 			</FieldValuePair>
> > 		</FieldValuePairs>
> > 	</xsl:template>
> > 
> > Thanks for any help!
> >  
> > Chris Bordeman

Current Thread