Re: [xsl] check for optional element

Subject: Re: [xsl] check for optional element
From: Jarkko.Moilanen@xxxxxx
Date: Thu, 22 Jan 2004 12:10:33 +0200
Lainaus Nikolas Nehmer <Nikolas.Nehmer@xxxxxxxxxxx>:

< Hi,
<
< I want to check if an optional element is present in my XML document and
< if so I want to react to this fact. For example I have an optional
< element subsidiary in my Organesation.xml. If a subsidiary is present I
< want to create an additional column in my HTML table. How could I
< realise this test. The element-available() function does only apply to
< instruction elements I think?!
<
< Best regards,
<
< Nick

Source tree:

<?xml version="1.0"?>
<root>
<elements>
 <foobar>
 <subsidiary>is here</subsidiary>
 </foobar>

 <foobar>
 only foobar
 </foobar>
</elements>
</root>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="/">
 <new_root>
  <xsl:for-each select="root/elements/foobar">
   <xsl:if test="subsidiary">
     <xsl:copy-of select="."/>
   </xsl:if>
  </xsl:for-each>
</new_root>
</xsl:template>

</xsl:stylesheet>



result:

<?xml version="1.0" encoding="utf-8"?>
<new_root><foobar>
 <subsidiary>is here</subsidiary>
 </foobar>
</new_root>


***************************************************
* Jarkko Moilanen                                 *
* Dokumentoinnin kehittäjä                        *
* Profound XML technology Expert                  *
* DokuMentori Oy                                  *
* www.dokumentori.fi                              *
***************************************************

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread