×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

(XSLT) Check if element node exists

(XSLT) Check if element node exists

(XSLT) Check if element node exists

(OP)
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

RE: (XSLT) Check if element node exists

<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>

RE: (XSLT) Check if element node exists

(OP)
Thanks a lot Tsuji, your solution works fine.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close