Xpath Find and Replace

Jan 1, 2011 at 5:48 PM

 

I have search high and low for this answer and thought I would ask this group.

I have this in my .aspx page:

<a class="iFrameMe" href="<%#XPath("media:player/@url", xmlN)%>">

I need to do a find and replace in it and I have trid many combos but cannot get it to work.  Here is how i believe it is supposed to be:

<a class="iFrameMe" href="<%#fn:replace(XPath("media:player/@url", xmlN), "find me", "replace me")%>">

but I keep getting this error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1026: ) expected

Source Error:


Line 6:              <ItemTemplate>
Line 7: <div class="gallery">
Line 8: <a class="iFrameMe" href="<%#fn:replace(XPath("media:player/@url", xmlN), "", "")%>"><img src="<%#XPath("media:thumbnail/@url", xmlN) %>" alt="<%#XPath("media:title", xmlN) %>" width="175px" /></a>
Line 9: <span>
Line 10: <b> <%#XPath("media:title", xmlN) %></b>


Source File: c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx    Line: 8


Show Detailed Compiler Output:

c:\windows\system32\inetsrv> "C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe" /t:library /utf8output /R:"C:\Windows\assembly\GAC_MSIL\System.Runtime.Serialization\3.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\c1a01ebe\a1f6f216_709ecb01\BlogEngine.Core.DLL" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\a1e46240\00496768_4374c901\Google.GData.Extensions.DLL" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_global.asax.4bz8nqlc.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_opc6_ncp.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorlib.dll" /R:"C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\51c5a47c\00f86e1e_299dcb01\BlogML.DLL" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Code.delna10b.dll" /R:"C:\Windows\assembly\GAC_64\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.WorkflowServices\3.5.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\858c8d78\00769869_4374c901\Google.GData.Client.DLL" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_GlobalResources.n4uhjmce.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Management\2.0.0.0__b03f5f7f11d50a3a\System.Management.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.IdentityModel\3.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\Windows\assembly\GAC_64\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel.Web\3.5.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\869bcf8d\00f86e1e_299dcb01\AjaxMin.DLL" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\347a9355\00769869_4374c901\Google.GData.Photos.DLL" /out:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_yaukpuxe.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701 /warnaserror-  "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_yaukpuxe.0.cs" "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_yaukpuxe.1.cs" "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_yaukpuxe.2.cs"


Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.4926
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,68): error CS1026: ) expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,68): error CS1525: Invalid expression term ':'
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,69): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,118): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,118): error CS1525: Invalid expression term ','
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,120): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,167): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,167): error CS1525: Invalid expression term ')'
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,168): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(8,168): error CS1525: Invalid expression term ')'
Is what I am trying to do possible?
Coordinator
Jan 1, 2011 at 7:14 PM

Try this line of code:

 

<a class="iFrameMe" href='<%#fn:replace(XPath("media:player/@url", xmlN), "", "")%>'>
<img src='<%#XPath("media:thumbnail/@url", xmlN) %>' alt='<%#XPath("media:title", xmlN) %>' width="175px" /></a>

I changed the quotation marks for HREF, SRC and ALT from double quotation marks to single quotation marks.  This is because within HREF, SRC and ALT, you have double quotation marks for the XPath and replace functions.  The compiler can't distinguish what content goes within each set of quotation marks.

Jan 1, 2011 at 9:56 PM

Ben,

Thanks for the response.  That didn't work either.  I got the same error.  I thought I would give the whole picture and see if that helps:

here is the .aspx code:

<%@ Page Language="C#" MasterPageFile="~/themes/thenetheryfamily/site.master" AutoEventWireup="true" CodeFile="youtube-search.aspx.cs" Inherits="XML_youtube_search" %>


<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" Runat="Server">
        <asp:Repeater ID="Repeater1" runat="server" EnableViewState="false">
            <ItemTemplate>
                <div class="gallery">
                        <a class="iFrameMe" href="<%#XPath("media:player/@url", xmlN)%>">
                        <img src='<%#XPath("media:thumbnail/@url", xmlN) %>' alt='<%#XPath("media:title", xmlN) %>' width="175px" /></a>
                        <span>
                       <b> <%#XPath("media:title", xmlN) %></b>
                        <%#XPath("media:description", xmlN)%>
                        </span>
                </div>
            </ItemTemplate>
        </asp:Repeater>
       </asp:Content>

and here is the code behind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Generic;
using System.Xml.XPath;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

using Resources;

using Page = BlogEngine.Core.Page;

public partial class XML_youtube_search : BlogBasePage
{
    // public object for XmlNamespaceManager
    public XmlNamespaceManager xmlN;

    protected void Page_Load(object sender, EventArgs e)
    {
        searchYoutube();
    }


    protected void searchYoutube()
    {
        // XmlDataSource Control Dynamic Object
        XmlDataSource xmlSource = new XmlDataSource();
        xmlSource.DataFile = "http://gdata.youtube.com/feeds/api/users/gndnethery/uploads?vq=gndnethery&orderby=published&start-index=1&max-results=50&alt=rss";


        // XmlNamespaceManager initialized by passing the Xml Document NameTable
        xmlN = new XmlNamespaceManager(xmlSource.GetXmlDocument().NameTable);
        xmlN.AddNamespace("media", "http://search.yahoo.com/mrss/");
        xmlN.AddNamespace("openSearch", "http://a9.com/-/spec/opensearchrss/1.0/");

        // XmlNodeList generated by passing XPath expression and XmlNamespaceManager Object
        XmlNodeList xmlNodes = xmlSource.GetXmlDocument().SelectNodes("rss/channel/item/media:group", xmlN);



        Repeater1.DataSource = xmlNodes;
        Repeater1.DataBind();
    }
}

Could all of this be moved to the code behind and make it easier.   Maybe something like a foreach  entry ???

Coordinator
Jan 1, 2011 at 10:04 PM

In the latest code you posted, the HREF value is still enclosed in double quotation marks.  The SRC and ALT values are correctly enclosed in single quotation marks.  So, just the HREF needs to be changed to single quotation marks -- see my last message, which has it in single quotation marks.

Jan 1, 2011 at 10:22 PM

Oops.  I in advertantly undid the change you suggested.  The line in questions should and does read:

<a class="iFrameMe" href='<%#fn:replace(XPath("media:player/@url", xmlN), "", "")%>'>
I am still getting the same error.

Coordinator
Jan 1, 2011 at 10:28 PM

Ok, no problem.  And the error you're getting is still "CS1026: ) expected" ?  And it's still highlighting the same line for where the error is happening?  Maybe try breaking up the A and IMG tags so they are on 3 separate lines, like this:

<a class="iFrameMe" href='<%#XPath("media:player/@url", xmlN)%>'>
<img src='<%#XPath("media:thumbnail/@url", xmlN) %>'
alt='<%#XPath("media:title", xmlN) %>' width="175px" /></a>

Then see which of the above lines the compiler complains about.  This will help us identify which of the 3 <%# ... %> tags where the ) is missing.

Jan 2, 2011 at 12:09 AM

Ok I have it in as:

 

<a class="iFrameMe" href='<%#fn:replace(XPath("media:player/@url", xmlN), "watch?v=", "embed/")%>'>
                        <img src='<%#XPath("media:thumbnail/@url", xmlN) %>'
                        alt='<%#XPath("media:title", xmlN) %>' width="175px" /></a>

 

I also tried:

<a class="iFrameMe" href='<%#fn:replace((XPath("media:player/@url", xmlN)), "watch?v=", "embed/")%>'>
                        <img src='<%#XPath("media:thumbnail/@url", xmlN) %>'
                        alt='<%#XPath("media:title", xmlN) %>' width="175px" /></a>

and in both cases the error is

Compiler Error Message: CS1026: ) expected

Source Error:


Line 10:                 <div class="gallery">
Line 11:
Line 12: <a class="iFrameMe" href='<%#fn:replace(XPath("media:player/@url", xmlN), "watch?v=", "embed/")%>'>
Line 13: <img src='<%#XPath("media:thumbnail/@url", xmlN) %>'
Line 14: alt='<%#XPath("media:title", xmlN) %>' width="175px" /></a>


Source File: c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx    Line: 12


Show Detailed Compiler Output:

c:\windows\system32\inetsrv> "C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe" /t:library /utf8output /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Management\2.0.0.0__b03f5f7f11d50a3a\System.Management.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_opc6_ncp.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\347a9355\00769869_4374c901\Google.GData.Photos.DLL" /R:"C:\Windows\assembly\GAC_64\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\858c8d78\00769869_4374c901\Google.GData.Client.DLL" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\a1e46240\00496768_4374c901\Google.GData.Extensions.DLL" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Code.delna10b.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\c1a01ebe\a1f6f216_709ecb01\BlogEngine.Core.DLL" /R:"C:\Windows\assembly\GAC_64\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_global.asax.4bz8nqlc.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_GlobalResources.n4uhjmce.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.IdentityModel\3.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.WorkflowServices\3.5.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorlib.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Runtime.Serialization\3.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\869bcf8d\00f86e1e_299dcb01\AjaxMin.DLL" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel.Web\3.5.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\assembly\dl3\51c5a47c\00f86e1e_299dcb01\BlogML.DLL" /R:"C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /out:"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_ta7sjoyp.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701 /warnaserror-  "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_ta7sjoyp.0.cs" "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_ta7sjoyp.1.cs" "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\34becf91\bf15eea\App_Web_ta7sjoyp.2.cs"


Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.4926
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,68): error CS1026: ) expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,68): error CS1525: Invalid expression term ':'
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,69): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,132): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,132): error CS1525: Invalid expression term ','
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,134): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,181): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,181): error CS1525: Invalid expression term ')'
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,182): error CS1002: ; expected
c:\inetpub\wwwroot\BlogEngine2.0\youtube-search.aspx(12,182): error CS1525: Invalid expression term ')'
Coordinator
Jan 2, 2011 at 12:26 AM

Is "fn:replace" a valid function?  I don't think I'm familiar with that syntax.  If you're trying to replace a substring within a string, you could try using the normal replace function, like:

<a class="iFrameMe" href='<%# XPath("media:player/@url", xmlN).ToString().Replace("watch?v=", "embed/") %>'>
Jan 2, 2011 at 12:45 AM

Well Duh...  I was not aware that you could do that in the page itself.  I had used this on another seciton in the code behind.  As soon as I saw your reply I knew you were correct.  As for fn:replace being a valid function...it is but I do not know how to use it properly as you can tell.

I ended up extending the replace as below and got exactly what I wanted.

THANKS!!!

<a class="iFrameMe" href='<%#XPath("media:player/@url", xmlN).ToString().Replace("watch?v=", "embed/").Replace("&feature=youtube_gdata_player", "?autoplay=1&rel=0") %>'>

Jan 2, 2011 at 2:08 AM

Once again, thanks Ben.

Finished product: http://thenetheryfamily.com/myvideos.aspx

If anyone is interested, I am glad to share.

Coordinator
Jan 2, 2011 at 2:36 AM

The page works very smoothly.  Looks like you switched to Colorbox.  Nice job.