tags:

views:

261

answers:

5

I want to check xml before displaying it .I am using XPath not xsl for it. For e.g.

<title></title>
<url></url>
<submit></submit>

i wanna check that if xml data is not there for it . Then don't display it. because I m putting these values in <a href=<%#container.dataitem,url%>>new link</a>. So i want that if url is empty then don't display new link otherwise display it and similarly for title that if title is not empty display it otherwise don't display it.

Main problem is I can check like in ascx.cs file if(iterator.current.value="") don't display it but the problem is in ascx file i m givin

<a href="">new link</a>

i want that new link should not come if url is empty... Any idea how to check this condition?

A: 

Use XPath. Assuming that the elements are enclosed in an element named link:

link[title != '' and url !='']

will find you the link elements whose title and url child elements contain no descendant text nodes. To make it a little more bulletproof,

link[normalize-space(title) != '' and normalize-space(url) !='']

will keep the expression from matching link elements whose title or url children contain whitespace.

Robert Rossney
I know i can find like that. In my case I was finding it using iterator.current.value="" or not but my problem is how to based my hyperlink on that .I mean if it is blank not display link and if it is not blank then display link. My expression for link is in ascx file as I have explained above.
AB
I think what Robert is suggesting would be to ignore all nodes without a url specified. It's not exactly clear how you're using title and submit in your post.
Jim Schubert
A: 

If you don't have access to the .cs file for this then you can still embed the code right in the .ascx file. Remember, you don't HAVE to put all your code in the code behind file, it can go inline right inside the .ascx file.

<% 
if(iterator.current.value!="") { 
%>
    <a href=<%#container.dataitem,url%>>new link</a>
<% 
}
%>
Mark Ewer
A: 

I've seen this handled using an asp:Literal control.

In the web form, you'd have <asp:Literal id='literal' runat='server' text='<%# GetAnchorTag(container.dataitem) %>' />

And in the code behind, you'd have:

protected string GetAnchorTag(object dataItem) {
  if(dataItem != null) {
    string url = Convert.ToString(DataBinder.Eval(dataItem, "url"));
    if(!string.IsNullOrEmpty(url)) {
      string anchor = /* build your anchor tag */
      return anchor;
    }
  }
  return string.Empty;
}

this way, you either output a full anchor tag or an empty string. I don't know how this would fit in with your title and submit nodes, but it solves the anchor display issue.

Personally, I don't like this approach, but I've seen it quite a bit.

Jim Schubert
This worked for me....I tried similar to this and its wroking perfectly thanks again Jim.........:-)
AB
A: 

Fire XPath and check for Empty string That's it..!!

Mital R
A: 

what about //a[not(./@href) or not(text()='']

funkymushroom