首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在内存中合并XML文档?

如何在内存中合并XML文档?
EN

Stack Overflow用户
提问于 2022-01-31 02:37:06
回答 1查看 307关注 0票数 1

我有两批文件。在批处理1中,当SPID等于特性/score/SPID时,我需要合并pit/score。预期的合并看起来像批处理2文档,特征/得分/默认值是pit/score的合并内容。如果文档已经有了characteristic/score/default,那么保持原样。

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

预期产出:

第1批:

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

我的xsl1不会合并任何东西,只会清除所有的pit元素。

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

我的xsl2失败了:多个项的序列不允许作为'eq‘(,,,.)的第二个操作数

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

我如何得到我的xsl工作?由于依赖关系,无法在xsl中使用。如果它是BaseX,那么它就是独立的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-31 03:54:59

XSLT 2

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

给定内存节点的

,使用XSLT 3是公平的。

代码语言:javascript
复制
    <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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70920726

复制
相关文章

相似问题

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