Re: [xsl] check for existance of an element in the xml

Subject: Re: [xsl] check for existance of an element in the xml
From: Raffaele Sena <raff@xxxxxxxxxxxx>
Date: Mon, 6 Dec 2004 21:17:12 -0800 (PST)
You can wrap your "script" tag in an if tag like:

<head>
  <xsl:if test="page/group/input">
    <script... />
  </xsl:if>
</head>

-- Raffaele

On Tue, 7 Dec 2004, Jake The Snake Briggs wrote:

> HI all
> I would to check for the existance of an element, and act upon it, 
> rather than act everytime the element is matched. What i mean is I have 
> xml that describes how a web page looks, and an xsl that transforms that 
> to html and javascript.
> 
> For example, i may have this :
> 
> <page>
>     <groupbox x="10" y="10">
>         <text value="Blaa Blaa" x="10" y="10"/>
>         <input x="10" y="20" name="foo"/>
>     </groupbox>
>    
>     <groupbox x="10" y="30">
>         <text value="Something Else" x="10" y="10"/>
>         <input x="10" y="20" name="bar"/>
>     </groupbox>
>    
>     <input x="10" y="50" name="gle"/>
> </page>
> 
> The issue is that at the moment i have some javascript that operates on 
> the input fields, so I am including it everytime I produce a page, but I 
> want to only include it iff the xml has at least one <input .../> 
> element. This is of course simplified, the real xml and the html it 
> produces is a lot more complicated, some of the html pages produced are 
> getting fairly large :)
> The xsl root match is where i am including all the various javascript 
> files. Here is a simplified version of my xslt, for those who dont do html :
> 
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>     <xsl:output method="html"/>
> 
>     <xsl:template match="/">
>         <html>
>             <head>
>                 <script language="JavaScript" 
> src="Shared/Javascript/validate.js"></script>
>             </head>
>             <body>   
>                 <xsl:apply-templates/>
>             </body>
>         </html>
>     </xsl:template>
>    
>     <xsl:template match="input">
>          <!-- input field code goes here -->
>     </xsl:template>
>     .
>     .
>     .
> </xsl:stylesheet>
> 
> I only want that <script language="JavaScript" 
> src="Shared/Javascript/validate.js"></script> part to appear once in the 
> resulting html iff the xml i am processing has at least one <input..../> 
> element. I figure i can parse the xml twice, but i cant work out how i 
> would only match unique elements the first time :) What sort of thing 
> should i be googleing for in order to solve this problem, it seems like 
> the sort of thing people would want to do....
> 
> Jake
> 
> 
> 
> !DSPAM:41b5349f255512010010124!

Current Thread