Newbie help- filtering one XML file with another
I need to merge, sort, and filter records from a master XML file based on data in another XML document (one of many). I want to capture the entire contents of a "header" section, and all node information that match by category. While this isn't the real situation, I think the Top seller by category Book example will illustrate.
Suppose I want to generate an XML file that list top seller books by category, based on the interests of each subscriber. That file will have a common greeting, and all the details for only the categories of Books the subscriber is interested in, sorted by their ranking. We have a master XML file that is structured something like this:
<Greeting>... stuff... </Greeting>
<Top Sellers>
<Top Seller>
<category>Art</category>
<Title>...</Title>
</Top Seller>
<Top Seller>
<category>Suspense</category>
<Title>...</Title>
</Top Seller>
<Top Seller>
<category>Chemical Engineering</category>
<Title>...</Title>
</Top Seller>
<Top Seller>
<category>Drama</category>
<Title>...</Title>
</Top Seller>
</Top Sellers>
Then, there is a subscriber specific file that looks something like this:
<USERPREFS>
<preference>
<Category>Suspense</Category>
<Rank>1</Rank>
</preference>
<preference>
<Category>Sports</Category>
<Rank>2</Rank>
</preference>
<preference>
<Category>Humor</Category>
<Rank>3</Rank>
</preference>
<preference>
</preference>
</USERPREFS>
The result I'm wanting to have is something like this
<Greeting>... stuff... </Greeting>
<Top Sellers>
<Top Seller>
<category>Suspense</category>
<Title>...<Title>
</Top Seller>
<Top Seller>
<category>Sorts</category>
<Title>...<Title>
</Top Seller>
<Top Seller>
<category>Humor</category>
<Title>...<Title>
</Top Seller>
</Top Sellers>
where the contents of the greeting node contains all the details in the first file, ranked based on the second, and the content of all the matching top sellers nodes is also in the result file.
There will be many different "preference files" that will be applied to the same XML "master", so hard-coding the file name in an XSLT doesn't seem to be an option.
Seems like I might need to transform the second XML file into an intermediate XSLT, then apply that one to the first file. If this is the best approach, I'm having a hard time visualizing what the intermediate XSLT should look like (thus far, my XSLT experience is limited to relatively simple "for each" type transforms).
A code snippet or link to another topic that has a working example would be greatly appreciated.
|