如何在数据库中匹配和合并XML?

How to match and merge XML in database?

我在一个集合中有以下混合文档。

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>

目前为了简化问题,我需要合并pit/score@B4当其SPID等于characteristic/score/SPID@B3时在MarkLogic中。

预期输出:

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

我们遇到了问题。我的 xsl 结果全是空白。

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

如何让 match/merge 在我的 xsl 中工作?请注意 B3 和 B4 是同一数据库中的不同文档。

在 for-each 的谓词过滤器中:

<xsl:for-each select="/creditRisk/pit/score[SPID eq spid]">

您想过滤 SPID 等于变量 $spid 的位置。没有 $ 它正在寻找兄弟元素 spid (不存在)。

应该是:

<xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
  <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:cts="http://marklogic.com/cts" xmlns:fff="schema://fc.fasset/functions"
    exclude-result-prefixes="#all" version="2.0">
    
    <xsl:function name="fff:mergeModelI">
        <xsl:param name="characterScore"/>
        <xsl:for-each select="$characterScore">  
            <xsl:variable name="spid" select="SPID"/>
            <xsl:variable name="query"
                select="cts:path-range-query('/creditRisk/pit/score/SPID', '=', $spid)"/>
            <xsl:variable name="model" 
                select="cts:search(fn:collection('collection-name')/creditRisk/pit/score, $query)"/>
            <xsl:choose>
                <xsl:when test="exists($model)">
                    <score>
                        <matched>merged</matched>
                        <default>
                            <xsl:copy-of select="$model/node()"/>
                        </default>
                        <xsl:copy-of select="node()"/>
                    </score>
                </xsl:when>
                <xsl:otherwise/>
            </xsl:choose>          
        </xsl:for-each>    
    </xsl:function>
    
    <xsl:template match="characteristic">    
        <characteristic> 
            <xsl:sequence select="fff:mergeModelI(score)"/> 
        </characteristic>
    </xsl:template> 
    
    <xsl:template match="node()">
        <xsl:apply-templates/>
    </xsl:template>   
    
  </xsl:transform>

如果您正在考虑持续集成,matched/merged 文档应该被引入另一个具有不同集合的数据库中。

上述和数据governance/auditing需要match/merge操作跟踪。在这里,我标记 <matched>merged</matched> 作为追索权。 matched/merged 文档可以根据标签填充到另一个数据库中。您可以设计更全面的规范化以满足您的需求。