PDA

Click to See Complete Forum and Search --> : Filtering Data in XML


lenin
Feb 20th, 2001, 10:35 AM
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.

Jerry Grant
Feb 20th, 2001, 10:50 AM
If the target browser is XML aware (IE5 or NS6) then you add the transformation file to the start of the XML

<?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:

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?

lenin
Feb 21st, 2001, 03:16 AM
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.

Jerry Grant
Feb 21st, 2001, 07:16 AM
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.

lenin
Feb 21st, 2001, 07:46 AM
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>

Jerry Grant
Feb 21st, 2001, 07:51 AM
Yeah, I understand that, but what should the new xml structure be?

<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.

lenin
Feb 21st, 2001, 08:01 AM
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.

Jerry Grant
Feb 21st, 2001, 08:37 AM
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:

<%
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 :cool:

lenin
Feb 21st, 2001, 08:43 AM
Many thanks Jerry.