XSLT合并和匹配值 [英] XSLT merging and matching values

查看:82
本文介绍了XSLT合并和匹配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目似乎已经超出我的舒适范围,并且需要一些(高级?)XSL处理。

I'm have a project that seems to have moved someway out of my comfort zone and requires some (advanced?) XSL processing.

我有以下两个项目示例XML文档:

I have the following two example XML documents:

Doc1

<instance>
    <InfBy1>Dr Phibes</InfBy1>
    <InfBy2>Dr X</InfBy2>
    <InfBy3>Dr Chivago</InfBy3>
</instance>

Doc2

KB_XMod_Modules>
    <Physician>Dr Phibes</Physician>
    <XModID>60</XModID>
</KB_XMod_Modules>
<KB_XMod_Modules>
    <Physician>Dr X</Physician>
    <XModID>61</XModID>
</KB_XMod_Modules>
<KB_XMod_Modules>
    <Physician>Dr Chivago</Physician>
    <XModID>62</XModID>
</KB_XMod_Modules>

我必须从Doc2中获取XModID值,并将其与Doc1中的关联名称(值)进行匹配。但是,另外一个复杂的问题是,这正在创建要加载到数据库中的记录,因此在我的情况下,Phibes博士在< InfBy1> 之内,但在另一条记录中,他可能会说< InfBy3> 。无论如何,所需的输出将是:

I have to grab the XModID value from Doc2 and match it with the associated name (value) in Doc1. One extra complication however is that this is creating records to load into a database so in my scenario Dr Phibes is within <InfBy1> but in another record he may be in say <InfBy3>. Anyway, the desired output would be:

<InfBy1>
    <items>
        <item>
            <label>Dr Phibes</label>
            <value>60</value>
        </item>
    </items>
</InfBy1>
<InfBy2>
    <items>
        <item>
            <label>Dr X</label>
            <value>61</value>
        </item>
    </items>
</InfBy2>
<InfBy3>
    <items>
        <item>
            <label>Dr Chivago</label>
            <value>62</value>
        </item>
    </items>
</InfBy3>

任何想法都值得赞赏...

Any ideas really appreciated...

谢谢

推荐答案

此转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kPhysByName" match="KB_XMod_Modules"
          use="Physician"/>

 <my:doc2>
    <KB_XMod_Modules>
        <Physician>Dr Phibes</Physician>
        <XModID>60</XModID>
    </KB_XMod_Modules>
    <KB_XMod_Modules>
        <Physician>Dr X</Physician>
        <XModID>61</XModID>
    </KB_XMod_Modules>
    <KB_XMod_Modules>
        <Physician>Dr Chivago</Physician>
        <XModID>62</XModID>
    </KB_XMod_Modules>
 </my:doc2>

 <xsl:template match="/">
  <result>
   <xsl:apply-templates/>
  </result>
 </xsl:template>

 <xsl:template match="/*/*[starts-with(name(), 'InfBy')]">
  <xsl:variable name="vCur" select="."/>
  <xsl:for-each select="document('')">
   <xsl:variable name="vMod" select="key('kPhysByName', $vCur)"/>
   <xsl:copy>
    <items>
     <item>
      <label><xsl:value-of select="$vMod/Physician"/></label>
      <value><xsl:value-of select="$vMod/XModID"/></value>
     </item>
    </items>
   </xsl:copy>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

应用于提供的第一个XML文档(包含第二个嵌入式-只是为了方便):

when applied on the first of the provided XML documents (it contains the second embedded -- just for convenience):

<instance>
    <InfBy1>Dr Phibes</InfBy1>
    <InfBy2>Dr X</InfBy2>
    <InfBy3>Dr Chivago</InfBy3>
</instance>

产生所需的正确结果:

<result xmlns:my="my:my">
    <items>
        <item>
            <label>Dr Phibes</label>
            <value>60</value>
        </item>
    </items>
    <items>
        <item>
            <label>Dr X</label>
            <value>61</value>
        </item>
    </items>
    <items>
        <item>
            <label>Dr Chivago</label>
            <value>62</value>
        </item>
    </items>
</result>

说明

此转换非常简单。为了方便起见,我们使用键,并且出于相同的原因,我们将第二个文档嵌入到XSLT样式表中。 在实际应用中,第二个文档将是独立的,唯一需要做的更改(从样式表中删除除外)将是替换

This transformation is pretty straight-forward. We are using keys for convenience and we are embedding the second document into the XSLT stylesheet for the same reason. In a practical application the second document will be stand-alone and the only change required (except removing it from the stylesheet) will be to replace:

  <xsl:for-each select="document('')">

具有:

  <xsl:for-each select="document('someURL')">

这篇关于XSLT合并和匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆