RE: [xsl] RE: Transform does not work using a check on variable

Subject: RE: [xsl] RE: Transform does not work using a check on variable
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 15 Jul 2006 06:21:59 +0100
> I am unable to figure out what is wrong with the following  XSLT.

The error message seems fairly explicit:

Error on line 31 column 9 of file:/c:/temp/test.xsl:
  SXXP0003: Error reported by XML parser: The element type "Packs" must be
terminated by the
  matching end-tag "</Packs>".

You're doing this:

      <xsl:if test="$blindLots1='0'">
      <Packs/>
      <xsl:variable name="blindLots1">1</xsl:variable>
      </xsl:if>   

and later this:

     <xsl:if test="$blindLots1=' 1 '">
      </Packs>     
      <xsl:variable name="blindLots2">1</xsl:variable>
      </xsl:if>  

which suggests that you're under the impression that you can write a start
tag and an end tag as two separate operations. You can't, for two reasons:
firstly an XSLT stylesheet must be a well-formed XML document. More
fundamentally, XSLT is a language that transforms trees into trees: in a
tree, an element node is a single indivisible object: you can't write half a
node to a tree.

You also seem to be confused about the nature of XSLT variables. You've got
two variables called blindlots1, which often indicates that you are thinking
of variables in terms of non-functional languages that permit assignment;
and you've got a variable called blindLots2 which is never referenced and
can never be referenced.

I imagine that you're trying to achieve some kind of grouping. Go to
http://www.jenitennison.com/xslt/grouping to see how it should be done.

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

> Thanks for any help.
> 
> XSL
> 
> <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet 
> version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>   <xsl:template match="/">
> <StudyData studyId="{CC/MessageBody/ProtocolCD}">
>   <SupplyManagement messageType="Shipment Details">
>     <ShipmentDetails id="">
>       <LocationRef></LocationRef>
>       <DestinationRef></DestinationRef>
>       <ShipmentDate><xsl:value-of
> select="CC/MessageHeader/CreationDateTime"/></ShipmentDate>      
>       <Lots>
>       <xsl:for-each 
> select="CC/MessageBody/Materials/Material/Lots/Lot">
>         <Lot>
>           <LotNumber><xsl:value-of select="LotID"/></LotNumber>   
>           <LotCountries>
>           <xsl:for-each select="LotCountries/Country">
>                 <LotCountry><xsl:value-of 
> select="CountryCode"/></LotCountry>
>           </xsl:for-each>               
>           </LotCountries>
>           <LotExpiryDate><xsl:value-of
> select="LotRetestDate"/></LotExpiryDate>
>         </Lot>
>       </xsl:for-each>
>       </Lots>
>     <xsl:variable name="blindLots1">0</xsl:variable >
>     <xsl:for-each 
> select="CC/MessageBody/Materials/Material/Lots/Lot"> 
>     <xsl:if test="current()/LotType='BLIND'" >     
>       <xsl:if test="$blindLots1='0'">
>       <Packs>
>       <xsl:variable name="blindLots1">1</xsl:variable>
>       </xsl:if>      
>         <xsl:for-each select="Kits/Kit">
>           <Pack replace="true">
>             <PackRef><xsl:value-of select="KitRandomID"/></PackRef>
>             <DrugData>
>               <DrugType><xsl:value-of
> select="../../../../MaterialID"/></DrugType>
>               <DrugDesc><xsl:value-of
> select="../../../../MaterialDescription"/></DrugDesc>
>             </DrugData>
>             <PackLotNumber><xsl:value-of 
> select="../../LotID"/></PackLotNumber>
>           </Pack>
>         </xsl:for-each>
>     </xsl:if>
>     </xsl:for-each> 
>      <xsl:if test="$blindLots1=' 1 '">
>       </Packs>     
>       <xsl:variable name="blindLots2">1</xsl:variable>
>       </xsl:if>      
>     </ShipmentDetails>
>   </SupplyManagement>
> </StudyData>
> </xsl:template>
> </xsl:stylesheet>
>  
> XML to be transformed
>  
> <?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?> <CC>
>    <MessageHeader>
>               <MessageID>7164</MessageID>

Current Thread