Results 1 to 9 of 9

Thread: Filtering Data in XML

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    Hi,
    can anyone provide pointers on how to filter data in XML. I have a simple XML based search engine, which passes a paramter from a web page.

    Based on the search I ( at the moment ) use ASP to check for the occurence of the search work in the XML file.

    However I am keen to implement the project with an XSL file. This means that I ( think I ) will have to filter the XML data prior to applying the XSL file, but have been unsuccessful up to date.

    At the moment I have:
    HTML Source:

    <html>
    <body>
    <script language="javascript">
    // Load XML
    var objFiler
    var xml = new ActiveXObject("Microsoft.XMLDOM")
    xml.async = false
    xml.load("database.xml")


    // Set variable selection



    // Load the XSL
    var xsl = new ActiveXObject("Microsoft.XMLDOM")
    xsl.async = false
    xsl.load("search.xsl")

    // Filter XML File
    //NEED SOURCE HERE


    // Transform
    document.write(xml.transformNode(xsl))
    </script>

    </body>
    </html>

    XML FILE:
    <CODE>
    <SOURCE ID="1">
    <LANGUAGE>VB</LANGUAGE>
    <KEYWORDS>ADO CONNECTION OBJECT</KEYWORDS>
    <PATH>../Connection.html</PATH>
    <URL>connection</URL>
    <DESCRIPTION>Simple Source for a generic ADO connection to SQL Server</DESCRIPTION>
    </SOURCE>
    <SOURCE ID="2">
    <LANGUAGE>VB</LANGUAGE>
    <KEYWORDS>ADO COMMAND OBJECT</KEYWORDS>
    <PATH>../Connection.html</PATH>
    <URL>command</URL>
    <DESCRIPTION>Simple Source for a generic ADO command with parameterised query variables</DESCRIPTION>
    </SOURCE>
    <SOURCE ID="3">
    <LANGUAGE>VB</LANGUAGE>
    <KEYWORDS>ERROR MODULE</KEYWORDS>
    <PATH>../Connection.html</PATH>
    <URL>Error</URL>
    <DESCRIPTION>Source for a generic Error Module</DESCRIPTION>
    </SOURCE>
    </CODE>


    XSL:
    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    <xsl:template match="/">
    <html>
    <body>
    <table border="0">
    <tr bgcolor = "Green">
    <th>URL</th>
    <th>Description</th>
    </tr>
    <xsl:for-each select="CODE/SOURCE">
    <tr>
    <td bgcolor = "Yellow"><xsl:value-of select="URL"/></td>
    <td bgcolor = "Yellow"><xsl:value-of select="DESCRIPTION"/></td>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>


    thanks in advance.

    Lenin.

  2. #2
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810

    Question Filter?

    If the target browser is XML aware (IE5 or NS6) then you add the transformation file to the start of the XML
    Code:
    <?xml-stylesheet type="text/xsl" href="myStylesheet.xsl"?>
    <CODE>
    	<SOURCE ID="1">
    		<LANGUAGE>VB</LANGUAGE>
    		<KEYWORDS>ADO CONNECTION OBJECT</KEYWORDS>
    		<PATH>../Connection.html</PATH>
    		<URL>connection</URL>
    		<DESCRIPTION>Simple Source for a generic ADO connection to SQL Server</DESCRIPTION>
    	</SOURCE>
    	<SOURCE ID="2">
    		<LANGUAGE>VB</LANGUAGE>
    		<KEYWORDS>ADO COMMAND OBJECT</KEYWORDS>
    		<PATH>../Connection.html</PATH>
    		<URL>command</URL>
    		<DESCRIPTION>Simple Source for a generic ADO command with parameterised query variables</DESCRIPTION>
    	</SOURCE>
    	<SOURCE ID="3">
    		<LANGUAGE>VB</LANGUAGE>
    		<KEYWORDS>ERROR MODULE</KEYWORDS>
    		<PATH>../Connection.html</PATH>
    		<URL>Error</URL>
    		<DESCRIPTION>Source for a generic Error Module</DESCRIPTION>
    	</SOURCE>
    </CODE>
    Otherwise this will transform it in code:
    Code:
    Public Function ApplyXSL( _
                             ByVal strXMLfile As String, _
                             ByVal strXSLfile As String _
                             ) As String
                             
        Dim strXMl As String
        Dim objXML As MSXML2.DOMDocument
        Dim strXSL As String
        Dim objXSL As MSXML2.DOMDocument
        
        '// Load the XML
        Set objXML = New MSXML2.DOMDocument
        strXMl = App.Path & "\" & strXMLfile
        objXML.Load strXMl
        
        '// Load the XSL
        Set objXSL = New MSXML2.DOMDocument
        strXSL = App.Path & "\" & strXSLfile
        objXSL.Load strXSL
        
        '// Transform to html
        ApplyXSL = objXML.transformNode(objXSL)
        
        Set objXML = Nothing
        Set objXSL = Nothing
        
    End Function
    I think this is what you meant?
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    Sorry Jerry,
    I must have phrased this wrong. I know I have to apply a stylesheet, ( from the source I provided ). What I want is a method to filter an existing XML file prior to applying the XSL file. Again, I have a search string which should match against each element in the XML file. If it matched I want to create new XML with this node.

    So, from the example XML I provided, if I search on the <KEYWORD> ERROR it would create some XML with 1 element.

    Hope this is cleat.

    Lenin.

  4. #4
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810

    Sorry lenin

    I understand your problem now, but it's not short answer.
    I'm a bit busy now but if I have time I will write some script to do this for you.
    Can you post an example of the XML nodes you want to be generated, containg which data from the original XML.
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    Jerry,
    many thanks. Find below a sample XML I'm currently using: I want to say search for a keyword from the <KEYWORDS> elements list and if it marches any of them it should be in the new XML.



    <CODE>
    <SOURCE ID="1">
    <LANGUAGE>VB</LANGUAGE>
    <KEYWORDS>ADO CONNECTION OBJECT</KEYWORDS>
    <PATH>../Connection.html</PATH>
    <URL>connection</URL>
    <DESCRIPTION>Simple Source for a generic ADO connection to SQL Server</DESCRIPTION>
    <SOURCE_CODE>
    <![CDATA[
    VISUAL BASIC CODE FOR ADO CONNECTION HERE ]]>
    </SOURCE_CODE>
    </SOURCE>
    <SOURCE ID="2">
    <LANGUAGE>VB</LANGUAGE>
    <KEYWORDS>ADO COMMAND OBJECT</KEYWORDS>
    <PATH>../Connection.html</PATH>
    <URL>command</URL>
    <DESCRIPTION>Simple Source for a generic ADO command with parameterised query variables</DESCRIPTION>
    <SOURCE_CODE>
    <![CDATA[
    VISUAL BASIC CODE FOR ADO COMMAND HERE ]]>
    </SOURCE_CODE>
    </SOURCE>
    <SOURCE ID="3">
    <LANGUAGE>VB</LANGUAGE>
    <KEYWORDS>ERROR MODULE</KEYWORDS>
    <PATH>../Connection.html</PATH>
    <URL>Error</URL>
    <DESCRIPTION>Source for a generic Error Module</DESCRIPTION>
    <SOURCE_CODE>
    <![CDATA[
    VISUAL BASIC CODE FOR ADO CONNECTION HERE ]]>
    </SOURCE_CODE>
    </SOURCE>
    </CODE>

  6. #6
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    Yeah, I understand that, but what should the new xml structure be?
    Code:
    <MYMATCH>
        <MYDATA>
        What text do you want here, from the orriginal XML
        </MYDATA>
    </MYMATCH>
    Please let me know what the Node names, child nodes should be named/organised.
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    I want the structure to remain the same. Just filtering the data. I had tried using selectnodes with paramters and trying to populate another XML DOM with this, but with negative outcomes.

  8. #8
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810

    Use the removeChild method!

    Ok, I think this will work.....
    The following is in VBScript, you can convert it to JScript if you prefer, Assuming the XML is the loaded XMLstream:
    Code:
    <%
        Dim matchNodes
        Dim oNode
        Dim pNode
        Dim gpNode
        Dim strSearch
        
        strSearch = "ERROR" '// Put your search text here
        
        Set matchNodes = xml.selectNodes("//KEYWORDS")
        
        If matchNodes.length > 0 Then
            
            '// Node is <KEYWORDS>
            For Each oNode In matchNodes
            
                '// Parent Node is <SOURCE>
                Set pNode = oNode.parentNode
                
                '// Grandparent Node is <CODE>
                Set gpNode = pNode.parentNode
                
                If InStr(1, oNode.Text, strSearch) = 0 Then
                    '// Remove <SOURCE>
                    gpNode.removeChild pNode
                End If
                
            Next oNode
            
        End If
        
        Set gpNode = Nothing
        Set pNode = Nothing
        Set oNode = Nothing
        Set matchNodes = Nothing
    %>
    Good Luck
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    Many thanks Jerry.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width