简体   繁体   English

如何在 memory XML 文档中合并?

[英]How to merge in memory XML documents?

I have 2 batches of documents.我有 2 批文件。 In batch 1, I need to merge pit/score when its SPID equals to characteristic/score/SPID.在第 1 批中,当 SPID 等于特征/分数/SPID 时,我需要合并pit/score。 The expected merge looks like batch 2 documents, characteristic/score/default is the merged content of pit/score .预期的合并看起来像第 2 批文档,character/score/default 是pit/score的合并内容。 If the document already has the characteristic/score/default then keep it as it is.如果文档已经具有characteristic/score/default ,则保持原样。

Batch 1:

    <creditRisk>
      <characteristic>
        <score>
            <LID>C123</LID>
            <SPID>106123</SPID>
            <Sector>Real Estate, Rental and Leasing</Sector>
        </score>
        <score>
            <LID>C470</LID>
            <SPID>127999</SPID>
            <Sector>Professional, Scientific and Technical Services</Sector>
        </score>
      </characteristic>
      <pit>
        <score>
            <LID>C123</LID>           
            <SPID>106123</SPID>
            <LTV>0.8654782868</LTV>
            <LGD>0.099571924</LGD>
            <Logarithm>-0.104884989</Logarithm>
        </score>
        <score>
            <LID>C470</LID>           
            <SPID>127999</SPID>
            <LTV>0.1950292239</LTV>
            <LGD>0.4296130401</LGD>
            <Logarithm>-0.561440272</Logarithm>
        </score>
      </pit>
    </creditRisk>

Btach 2:
 
    <creditRisk>
      <characteristic>
        <score>
            <default>
                <LID>C307</LID>
                <SPID>363553</SPID>
                <LTV>1.031735135</LTV>
                <LGD>0.4421581764</LGD>
                <Logarithm>-0.583679827</Logarithm>
            </default>
            <LID>C307</LID>
            <SPID>363553</SPID>
            <Sector>Manufacturing and Consumer Products</Sector>
        </score>
        <score>
            <default>
                <LID>C357</LID>
                <SPID>329924</SPID>
                <LTV>0.8326657196</LTV>
                <LGD>0.1983093536</LGD>
                <Logarithm>-0.221032473</Logarithm>
            </default>
            <LID>C357</LID>
            <SPID>329924</SPID>
            <Sector>Utilities, Construction and Other Infrastructure</Sector>
        </score>
      </characteristic>
    </creditRisk>

Expected Output:预期 Output:

Batch 1:第 1 批:

<characteristic>
   <score>
      <default>
         <LID>C123</LID>
         <SPID>106123</SPID>
         <LTV>0.8654782868</LTV>
         <LGD>0.099571924</LGD>
         <Logarithm>-0.104884989</Logarithm>
      </default>
      <LID>C123</LID>
      <SPID>106123</SPID>
      <Sector>Real Estate, Rental and Leasing</Sector>
   </score>
   <score>
      <default>
         <LID>C470</LID>
         <SPID>127999</SPID>
         <LTV>0.1950292239</LTV>
         <Recovery>0.5703869599</Recovery>
         <LGD>0.4296130401</LGD>
         <Logarithm>-0.561440272</Logarithm>
      </default>
      <LID>C470</LID>
      <SPID>127999</SPID>
      <Sector>Professional, Scientific and Technical Services</Sector>
   </score>
</characteristic>

My xsl1 doesn't merge anything but wipe out all of the pit elements.我的 xsl1 没有合并任何东西,而是清除了所有的pit元素。

    <xsl:template match="creditRisk">
        <characteristic>
          <xsl:for-each select="characteristic/score">
            <score>
                <default>
                    <xsl:for-each select="pit/score[SPID eq ./SPID]">
                      <xsl:copy-of select="./*"/>
                    </xsl:for-each>
                </default>
                <xsl:copy-of select="./*"/>
            </score>                   
          </xsl:for-each>
        </characteristic>
    </xsl:template>

My xsl2 failed with error: A sequence of more than one item is not allowed as the second operand of 'eq' (, , , ...)我的 xsl2 因错误而失败:不允许将多个项目的序列作为 'eq' (, , , ...) 的第二个操作数

     <xsl:template match="creditRisk">
        <characteristic>
            <xsl:variable name="character" select="characteristic/score"/>
            <xsl:variable name="pit" select="pit/score"/>
            <xsl:choose>
                <xsl:when test="fn:exists($pit)">
                    <xsl:for-each select="$character">
                        <score>
                            <default>
                                <xsl:for-each select="$pit[SPID eq $character/SPID]">
                                    <xsl:copy-of select="./*"/>
                                </xsl:for-each>
                            </default>
                            <xsl:copy-of select="$character/*"/>
                        </score>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </characteristic>
    </xsl:template>

How can I get my xsl work?我怎样才能让我的 xsl 工作? I can't use <xsl:template match="/"> in the xsl due to the dependency.由于依赖关系,我不能在 xsl 中使用 <xsl:template match="/"> 。 If it is BaseX, then it is freestanding.如果是 BaseX,那么它是独立的。

XSLT 2 XSLT 2

<xsl:template match="creditRisk[pit]">
        <characteristic>
            <xsl:call-template name="score">
                <xsl:with-param name="characterScore" select="characteristic/score"/>
                <xsl:with-param name="pitScore" select="pit/score"/>
            </xsl:call-template>
        </characteristic>
    </xsl:template>
    
    <xsl:template match="creditRisk[not(pit)]">
        <xsl:copy-of select="./*"/>
    </xsl:template>
    
    <xsl:template name="score">
        <xsl:param name="characterScore"/>
        <xsl:param name="pitScore"/>
        <xsl:for-each select="$characterScore">
            <xsl:variable name="character" select="."/>
            <score>
                <default>
                    <xsl:for-each select="$pitScore[SPID eq $character/SPID]">
                        <xsl:copy-of select="./node()"/>
                    </xsl:for-each>
                </default>
                <xsl:copy-of select="$character/node()"/>
            </score>                    
        </xsl:for-each>
    </xsl:template>

Given memory node, it is only fair to have a go at XSLT 3给定 memory 节点,在 XSLT 3

    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:template match="creditRisk[pit]">
        <characteristic>
            <xsl:call-template name="score">
                <xsl:with-param name="characterScore" select="characteristic/score"/>
                <xsl:with-param name="pitScore" select="pit/score"/>
            </xsl:call-template>
        </characteristic>
    </xsl:template>
    
    <xsl:template match="creditRisk[not(pit)]">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template name="score">
        <xsl:param name="characterScore"/>
        <xsl:param name="pitScore"/>
        <xsl:for-each select="$characterScore">
            <xsl:variable name="character" select="."/>
            <score>
                <default>
                    <xsl:for-each select="$pitScore[SPID eq $character/SPID]">
                        <xsl:apply-templates/>
                    </xsl:for-each>
                </default>
                <xsl:apply-templates/>
            </score>                    
        </xsl:for-each>
    </xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM