locked
xslt: how can I fine-tune the href attribute of an anchor tag? RRS feed

  • Question

  • User846546683 posted

    I have a successful XSLT file (shown below), but what I'd like to do is adjust 

    <a href="{@url}">

    so that when when the transformed xml file appears in my asp:xml control on my aspx page, it appears as:

    <a href="<%# Page.ResolveClientURL(""{@url}"") %>">

    figured this wouldn't work, but this is the result I need. Is there some way to do this right?

    Here is the full xslt script, with the relevant line bolded:

    Thanks for any ideas.

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:map="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"
    exclude-result-prefixes="map">
    
        <xsl:output method="xml"  version="1.0" encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
        <xsl:strip-space elements="*"/>   
        
        <xsl:template  match="map:siteMapNode">
            <li>
                <a href="{@url}">
                    <xsl:value-of select="@title"/>
                </a>
                <xsl:if test="map:siteMapNode">
                    <xsl:call-template name="mapNode"/>
                </xsl:if>
            </li>
        </xsl:template>
    
        <xsl:template name="mapNode" match="map:siteMap">
            <xsl:choose>
                <xsl:when test="@*">
                    <ul>
                        <xsl:apply-templates />
                    </ul>
                </xsl:when>
                <xsl:otherwise>                
                    <ul id="">
                        <xsl:apply-templates />
                    </ul>                
                </xsl:otherwise>                
            </xsl:choose>
            
        </xsl:template>
    
    </xsl:stylesheet>

    Sunday, May 3, 2015 11:58 PM

Answers

  • User846546683 posted

    The key isn't to do this in the XSLT document, but rather the ASP.NET page.

    <script runat="server">
        Public MenuList As String = String.Empty
        Protected Sub Page_Load(sender As Object, e As EventArgs)
            Dim ds As New DataSet()
            ds.ReadXml(Server.MapPath("~/Web.sitemap"))
            
            ' this for-each block resolves client URL
            
            For Each dr As DataRow In ds.Tables(0).Rows
                Dim tempURL As String = Page.ResolveClientUrl(dr("url").ToString)
                dr("url") = tempURL
            Next
            
            Dim strXML As String = ds.GetXml()
            MenuList = GetHtml(Server.MapPath("~/sitemap_static_3.xslt"), strXML)
        End Sub
    
        Public Shared Function GetHtml(xsltPath As String, xml As String) As String
            Dim stream As New MemoryStream(ASCIIEncoding.[Default].GetBytes(xml))
            Dim document As New XPathDocument(stream)
            Dim writer As New StringWriter()
            Dim transform As New XslCompiledTransform()
            transform.Load(xsltPath)
            transform.Transform(document, Nothing, writer)
            Return writer.ToString()
        End Function
    </script>


    In the body: 

    <div id="content">
            <ul>
                <li>
                    <a href='<%= Page.ResolveUrl("~/index.aspx")%>'>Homepage</a>
                </li>
                <%= MenuList%>
            </ul>
        </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 4, 2015 1:10 PM