Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(XSLT) Check if element node exists 1

Status
Not open for further replies.

mfrmfr

Programmer
Jul 28, 2006
4
0
0
BE
Hi,

In an XSLT template, I'm trying to import an element from an external XML file, and if the element does not exist in the external file, then I add it myself.

What I did was a copy-of(xpath to externale doc), and that works fine. However, after that I do a test to check whether the said element exitsts or not (xsl if "count(element)=0").

And there's the problem, the if seems to always evaluate to true and then I have duplicate elements when the copy-of actually returns something.

Here's the troubling code (XSLT 1.0) :

Code:
<xsl:copy-of select="document('TestDoc.xml')//form[@templateref=$formname]/item[@templateref=$itemname]/data"/>
<xsl:if test="count(data)=0">
  <xsl:element name="data">
  <xsl:element name="possiblevalues00"/>
  <xsl:element name="uservalues00"/>
  </xsl:element>
</xsl:if>

I'm sure I'm missing something here...
Any suggestion would be more than welcome.
Thanks a lot.

MFR
 
[tt]<xsl:variable name="nset_ext" select=="document('TestDoc.xml')//form[@templateref=$formname]/item[@templateref=$itemname]/data"/>

<xsl:if test="$nset_ext">
<xsl:copy-of select="$nset_ext" />
</xsl:if>
<xsl:if test="not($nset_ext)">
<xsl:element name="data">
<xsl:element name="possiblevalues00"/>
<xsl:element name="uservalues00"/>
</xsl:element>
</xsl:if>
[/tt]
 
Thanks a lot Tsuji, your solution works fine.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top