Subject: ADVICE WANTED: Which method is more efficient?
Posted By: kwilliams Post Date: 8/29/2006 2:33:45 PM
I currently have a site that uses ASP.NET, VB.NET, XML, and XSLT using XSL Transformation in ASP.NET. My current method is this:
<script runat="server">
    Sub Page_Load
             'Pull page_qs querystring variable
             Dim page_qs As String = Request.QueryString("page")
             'Load XML file w/page_qs variable selected and assign page properties
             Dim il_xmld As XmlDocument
             Dim il_nodelist As XmlNodeList
             Dim il_node As XmlNode
             'Create the XML Document
             il_xmld = New XmlDocument()
             'Load the Xml file
             il_xmld.Load("links.xml")
             'Get the list of name nodes
             il_nodelist = il_xmld.SelectNodes("/links/page[@id = '" & page_qs & "']")
             'Loop through the nodes
             For Each il_node In il_nodelist
    
             'Get an Attribute Value
             Dim page_id = il_node.Attributes.GetNamedItem("id").Value
    
             'Pull XML nodes
             dir_code = il_node.Item("dir_code").InnerText
             subdir1_code = il_node.Item("subdir1_code").InnerText
             subdir2_code = il_node.Item("subdir2_code").InnerText

             'Page path creator
             'If dir is empty
             path_slash = "/"
             If dir_code = "" Then
                 dir_path = ""
             Else
                 dir_path = dir_code + path_slash
             End If
    
             'If subdir1 is empty
             If subdir1_code = "" Then
                 subdir1_path = ""
             Else
                 subdir1_path = subdir1_code + path_slash
             End If
    
             'If subdir2 is empty
             If subdir2_code = "" Then
                 subdir2_path = ""
             Else
                 subdir2_path = subdir2_code + path_slash
             End If
    
             'If page is not empty
             If page_id <> "" Then
                 page_path = page_id
             End If

             'Declare XML and XSL file paths
             Dim xmlURL As String, xslURL As String
             xmlURL = "/DIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xml/" + page_path + ".xml"
             xslURL = "/DIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xslt/" + page_path + ".xsl"
             'Assign dynamic url for this page
             xslTransform.DocumentSource = xmlURL
             xslTransform.TransformSource = xslURL
    
             'Load XML
             Dim xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
             xml.async = false
             xml.load(xmlURL)
    
             'Load XSL
             Dim xsl = Server.CreateObject("MSXML2.DOMDocument.3.0")
             xsl.async = false
             xsl.load(xslURL)

    End Sub
</script>
<html>
<body>
    <form runat="server">
        <asp:Xml id="xslTransform" runat="server"></asp:Xml>
    </form>
</body>
</html>


But I was wondering, would it be more or less efficient for me to create a function that transforms the XML and XSLT files in the code using the transformNode method by calling the function from a label in the form tag? I guess I'm thinking something like this:
<script runat="server">
    Sub Page_Load
        Function webdata
            'Pull page_qs querystring variable
            Dim page_qs As String = Request.QueryString("page")
            'Load XML file w/page_qs variable selected and assign page properties
            Dim il_xmld As XmlDocument

            '.... SAME AS ABOVE
            'Load XML
            Dim xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
            xml.async = false
            xml.load(xmlURL)
    
            'Load XSL
            Dim xsl = Server.CreateObject("MSXML2.DOMDocument.3.0")
            xsl.async = false
            xsl.load(xslURL)

            'Transform file
            webdata.Text = xml.transformNode(xsl)
        End Function
    End Sub
</script>
<html>
<body>
    <form runat="server">
        <asp:Label id="webdata" runat="server" />
    </form>
</body>
</html>


NOTE: Please keep in mind that I'm a newbie to all of this, so I realize that my logic may not be correct in the second example.

Or is the difference so minor that it doesn't matter? I'd greatly appreciate some input on this. Thanks for any advice.

KWilliams
Reply By: thenoseknows Reply Date: 8/30/2006 12:28:03 AM
What are you trying to do here? Are you just trying to display the XML document that you loaded, or does the XSLT transform that XML into HTML? Whats the point of this?

 



Neil Timmerman
Programmer
Veris Consulting
Reply By: kwilliams Reply Date: 8/30/2006 4:58:28 PM
It transforms the XML and XSLT docs into HTML. I included the following code in my previous post for example #1, which was a mistake:
'Load XML
             Dim xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
             xml.async = false
             xml.load(xmlURL)
    
             'Load XSL
             Dim xsl = Server.CreateObject("MSXML2.DOMDocument.3.0")
             xsl.async = false
             xsl.load(xslURL)

This script pulls the data from a central XML doc, that is set up like this:
<internal_links>
<page id="1">
<title>Page 1</title>
<subdir1>main</subdir1>
...
</page>
<page id="2">
<title>Page 2</title>
<subdir1>sub</subdir1>
...
</page>
</internal_links>

and then pulls corresponding XML and XSLT docs depending on the first XML doc's properties. So it's like this:
xmlURL = page_path + subdir1 + "docs/xml" + pagename + ".xml"
xslURL = page_path + subdir1 + "docs/xslt" + pagename + ".xsl"

Which results in these two files being transformed:
xmlURL = "/DIRECTORY/docs/xml/page1.xml"
xslURL = "/DIRECTORY/docs/xslt/page1.xsl"

This all results in a transformation of the XML and XSLT docs for the entire site from one ASP.NET page. I hope that this makes more sense.

KWilliams
Reply By: thenoseknows Reply Date: 8/31/2006 11:02:51 AM
Ok, so is the primary difference between these two versions, is that one loops through "links.xml" and transforms each page for the whole site on the first page_load event of the first page the user hits, where as the second version transforms each page as they hit them, and you figure out which page they are on by a query string argument, right?

By the way I never found the end of the For Each loop in the first version. I also don't understand why the Function is inside the page_load event handler on the second version. Is that a VB thing? Also, don't functions have to have arguments in VB, so shouldn't that be a sub?

I am a bit of a "newb" to VB so pardon my dumb questions. I have quite a bit more experience with C, C++ and now C#.

TO get back to your original question of efficiency, if I understand what you are doing correctly, I think knowing which approach is more efficient is highly depentant on your users. Loading the links.xml one time, and transforming every xml document for every page in one single page load incurs unnecessary cost if the user never visits any of those other pages, or very few of them. If the user visits most pages, or a large number of pages, then perhaps it's worth it. Without knowing more I would go for the second approach, under the assumption that you simply don't know what the average user will do and how many pages they will visit, and this spreads the cost evenly yeilding a more consistent page load time. But this is all just back-of-the-envelope conjecture so take it for what its worth.

Neil Timmerman
Programmer
Veris Consulting

Go to topic 49126

Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182
Return to index page 181
Return to index page 180
Return to index page 179