'How to match and merge XML in database?
I have below mixed documents in one collection.
B3:
<creditRisk>
<characteristic>
<score>
<LID>C230</LID>
<SPID>129587</SPID>
<Sector>Finance and Insurance</Sector>
</score>
<score>
<LID>C177</LID>
<SPID>360720</SPID>
<Sector>Mining and Oil and Gas Extraction</Sector>
</score>
</characteristic>
</creditRisk>
B4:
<creditRisk>
<pit>
<score>
<LID>C230</LID>
<SPID>129587</SPID>
<LTV>1.4689503</LTV>
<LGD>0.5995806998</LGD>
<Logarithm>-0.915243031</Logarithm>
</score>
<score>
<LID>C177</LID>
<SPID>360720</SPID>
<LTV>1.524224737</LTV>
<LGD>0.8989534312</LGD>
<Logarithm>-2.292173791</Logarithm>
</score>
</pit>
</creditRisk>
At the moment to simplify the problem, I need to merge pit/score@B4 when its SPID equals to characteristic/score/SPID@B3 inside MarkLogic.
Expected Output:
<characteristic>
<score>
<default>
<LID>C230</LID>
<SPID>129587</SPID>
<LTV>1.4689503</LTV>
<LGD>0.5995806998</LGD>
<Logarithm>-0.915243031</Logarithm>
</default>
<LID>C230</LID>
<SPID>129587</SPID>
<Sector>Finance and Insurance</Sector>
</score>
<score>
<default>
<LID>C177</LID>
<SPID>360720</SPID>
<LTV>1.524224737</LTV>
<LGD>0.8989534312</LGD>
<Logarithm>-2.292173791</Logarithm>
</default>
<LID>C177</LID>
<SPID>360720</SPID>
<Sector>Mining and Oil and Gas Extraction</Sector>
</score>
</characteristic>
We are facing issue. My xsl comes out all blank results.
<xsl:template match="characteristic">
<characteristic>
<xsl:call-template name="scoreSPID">
<xsl:with-param name="characterScore" select="score"/>
</xsl:call-template>
</characteristic>
</xsl:template>
<xsl:template name="scoreSPID">
<xsl:param name="characterScore"/>
<xsl:for-each select="$characterScore">
<xsl:variable name="spid" select="SPID"/>
<score>
<xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
<default>
<xsl:copy-of select="./node()"/>
</default>
<xsl:copy-of select="node()"/>
</xsl:for-each>
</score>
</xsl:for-each>
</xsl:template>
<xsl:template match="node()">
<xsl:apply-templates/>
</xsl:template>
How can I get the match/merge work in my xsl? Do note B3 and B4 are different dokuments in the same database.
Solution 1:[1]
In the predicate filter for the for-each:
<xsl:for-each select="/creditRisk/pit/score[SPID eq spid]">
you want to filter where the SPID is equal to the variable $spid. Without the $ it is looking for a sibling element spid (which doesn't exist).
It should be:
<xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mads Hansen |
