首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在数据库中匹配和合并XML?

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

Stack Overflow用户
提问于 2022-01-31 19:14:48
回答 2查看 359关注 0票数 2

我把下面的混合文件集中在一个集合中。

代码语言:javascript
复制
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等于MarkLogic内的characteristic/score/SPID@B3时,我需要合并它。

预期产出:

代码语言:javascript
复制
<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结果都是空白的。

代码语言:javascript
复制
    <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>

如何在xsl中获得匹配/合并工作?请注意,B3和B4在同一个数据库中是不同的功能。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-04 16:06:21

代码语言:javascript
复制
  <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</matched>标记为一种追索权。匹配/合并的文档可以基于标记在另一个数据库中填充。您可以设计更全面的规范化,以适应您的需要。

票数 1
EN

Stack Overflow用户

发布于 2022-02-01 04:20:54

在for -each的谓词筛选器中:

代码语言:javascript
复制
<xsl:for-each select="/creditRisk/pit/score[SPID eq spid]">

您希望在SPID等于变量$spid的地方进行筛选。没有$,它正在寻找同级元素spid (它不存在)。

它应该是:

代码语言:javascript
复制
<xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70931597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档