Quote:
|
quote:I suspect you want to do parallel tree-walking down the two trees....it involves some kind of process where you copy children that are in one document but not the other, and call yourself recursively for children that "exist" in both documents.
|
Yes, but don't I need some way to dynamically store and create location paths so I can check the Target for nodes in the Source? After all, that "existence" is defined by having the same location paths but for different roots.
I have figured out copying from the Source for "inheriting" nodes, but I'm using the hard-coded path path expression (<<geniuses/genius>>), predicated for a particular text value, suggested by Sam. (Thanks for that.)
So, is there a way to 1) store a node's location path into a variable and 2) construct a location path for the second document using the location path stored in the variable asked for in 1)? Does that construction require stripping of the root step of the target node and replacing it with the root step returned by the document function?
With those location path variable I can walk the Source, testing for and copying corresponding Target children and otherwise taking the source. I'm thinking of something like <<"document('Source.xml')/{variable specifying location path}[text()={variable with text of target node}]>>. Without that dynamic location path construction and testing, I really don't know how to do this.
Here is one code that copies out the source nodes. (Please note that the example documents posted above have an error -- I've reposted the corrected documents below.)
<xsl:variable name="source" select="document('Source.xml')"/>
<xsl:template match="node()[not(attribute::inherits_from)]">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()[attribute::inherits_from]">
<xsl:variable name="crnt" select="normalize-space(text())"/>
<xsl:apply-templates select="$source/geniuses/genius[normalize-space(text())=$crnt]"/>
</xsl:template>
I suppose I could use set operators: an <<except>> to get all Source nodes not in the Target, and then a <<union>> (?) to combine those unique Source nodes with the Target nodes. But simply outputting that list would get the nodes, without any nesting.
I apologize again for the long question. I've spent the past two days in Michael's book, and in "Beginning XML", and I'm learning a ton, but I'm still clearly missing something. Thanks.
======================================
Corrected example documents:
Source
<geniuses>
<genius>Turing
<field>Mathematics</field>
<contribution>Turing Machines</contribution>
</genius>
<genius>Godel
<field>Logic</field>
<contribution>No complete system can be consistent</contribution>
<annoyed>Bertrand Russell</annoyed>
</genius>
</geniuses>
Target
<geniuses>
<note>Where would we be without these guys?</note>
<genius inherits_from="Source">Turing
<contribution>Broke Enigma</contribution>
<fictionalized_in>Cryptonomicon</fictionalized_in>
</genius>
<genius inherits_from="Source">Godel</genius>
<genius>Neumann</genius>
</geniuses>