aspose file tools*
The moose likes XML and Related Technologies and the fly likes Get data by Matching ID's from two different XML documents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Get data by Matching ID Watch "Get data by Matching ID New topic
Author

Get data by Matching ID's from two different XML documents

Tanya Rawat
Ranch Hand

Joined: Jul 23, 2001
Posts: 31
Could anyone suggest me what would be the best way to achieve this result:
Ther are two XML documents and in the first one there is a link which carries only the ID for a particular element type and in the other XML document which has IDs for a particular element type, same as previous document, as well as the associated details with that element. Now through the link from first XML document and by matching the ID passed by the first document I want to display the associated contents from the second XML document in another window. I hope i explained it clearly.
So XMl documents would be like this, just prototype:
example1.xml
<!-- There can be two or more types of Type attributes. IDs will be unique for each type of Node, but may be same for two different type of Nodes. -->
<Node Type="FirstType" ID="1"/> <!-- has ID 1 -->
<Node Type="SecondType" ID="1"/> <!-- also has ID 1 but different Type -->
<Node Type="FirstType" ID="2"/>
<Node Type="FirstType" ID="3"/>
<Node Type="SecondType" ID="2"/>
example2.xml
<Node Type="FirstType" ID="1">Content for FirstType of Node ID 1</Node>
<Node Type="SecondType" ID="1">Content for SecondType of Node ID 1</Node>
<Node Type="FirstType" ID="2">Content for FirstType of Node ID 2</Node>
<Node Type="FirstType" ID="3">Content for FirstType of Node ID 3</Node>
<Node Type="SecondType" ID="2">Content for SecondType of Node ID 4</Node>
I'm not clear how to use and whether to use XPointers and Xlink here. Any resources for examples on XPointers and Xlink....
Thanks

[This message has been edited by Tanya Rawat (edited August 14, 2001).]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
First of all, you need to read your second XML document. You can do that with document() function:
<xsl:variable name="secondDocument" select="document('example2.xml')"/>
Now you can query the second document by addressing the "secondDocument" variable. To match Types and IDs you can have a simple template:
<xsl:template match="Node">
<xsl:variable name="type" select="@Type"/>
<xsl:variable name="id" select="@ID"/>
<xsl:value-of select="$secondDocument//Node[@Type=$type and @ID=$id]"/>
</xsl:template>
However, this algorithm makes XSLT processor to search the second document every time the first document has a reference to it. This process is slow. If your documents are small, you will not notice any significant delay so this approach will be sufficient. But if you have huge documents, the delay may be unacceptable. Then you have no better choice than using key() function. When you define your keys, XSLT processor builds an index in memory to perform a lookup, and such a lookup is very fast.
Hard part is to make key() function work on another document. Fortunately, Bob DuCharme nicely provided us with an example of how this can be done I changed his xq487.xsl to match your example and also changed your example (a little) to match his XSLT I added root elements and changed Node element to aNode in the second XML to avoid confusion.
XSL: (shamelessly plagiarized)
<!-- xq487.xsl: converts xq484.xml into xq493.xml -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:variable name="secondDoc" select="document('xq485.xml')"/>
<xsl:key name="lookupID" match="aNode" use="@ID"/>
<xsl:template match="root">
<xsl:apply-templates select="$secondDoc"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root1"/>
<xsl:template match="Node">
<xsl:variable name="currentNodeID" select="@ID"/>
<xsl:for-each select="$secondDoc">
<xsl:value-of select="key('lookupID',$currentNodeID)"/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
XML:
<root>
<Node Type="FirstType" ID="1"/>
<Node Type="SecondType" ID="1"/>
<Node Type="FirstType" ID="2"/>
<Node Type="FirstType" ID="3"/>
<Node Type="SecondType" ID="2"/>
</root>
and the second XML:
<root1>
<aNode Type="FirstType" ID="1">Content for FirstType of Node ID 1</aNode>
<aNode Type="SecondType" ID="1">Content for SecondType of Node ID 1</aNode>
<aNode Type="FirstType" ID="2">Content for FirstType of Node ID 2</aNode>
<aNode Type="FirstType" ID="3">Content for FirstType of Node ID 3</aNode>
<aNode Type="SecondType" ID="2">Content for SecondType of Node ID 4</aNode>
</root1>

[This message has been edited by Mapraputa Is (edited August 14, 2001).]


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Tanya Rawat
Ranch Hand

Joined: Jul 23, 2001
Posts: 31
Thanks Mapraputa for your valuable response. But is there any way of passing the value of parameters with a queryString of a hyperlink to a XSL document and then matching that passed ID i can display the associated details of that ID in another window, not in the same window. Like we do in other dynamic server side applications.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Everything is possible with dynamic server side applications...
You can transform your <Node Type="FirstType" ID="1"/> to something like
<a href="someJsp.jsp?id=1" target="_blank">
and then in someJsp.jsp call another XSLT and pass it a param.
How do you pass a parameter depends on XSLT processor you use. Xalan, for example, provides Transformer's setParameter(String name, Object value) method.
Then you have to declare this parameter in your XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="id" select="'default value'"/>[/b]
and then refer to it as "$id" whenever it's needed.
[This message has been edited by Mapraputa Is (edited August 17, 2001).]
Tanya Rawat
Ranch Hand

Joined: Jul 23, 2001
Posts: 31
Thanks Mapraputa !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get data by Matching ID's from two different XML documents