使用XSLT将数据从XML节点合并到另一个节点

时间:2011-02-12 16:34:54

标签: xml xslt

使用XSLT,我试图找出如何使用来自另一组节点的数据合并/更新一组节点中的数据。节点具有相同的模式,但父节点不同。需要基于共享父属性合并数据。在下面的示例中,数据正从Principal复制到Driver。任何人都可以帮助我吗?

输入文件:

<Info>
  <Principal id="Insured">
    <PersonName>
      <GivenName>Jane</GivenName>
      <OtherGivenName>A</OtherGivenName>
      <Surname>Doe</Surname>
    </PersonName>
    <PersonInfo>
      <BirthDate>01-01-1980</BirthDate>
      <MaritalStatus>M</MaritalStatus>
    </PersonInfo>
    <PrincipalInfo></PrincipalInfo>
  </Principal>
  <Policy>
    <Driver id="Insured">
      <PersonName>
        <GivenName>Jane</GivenName>
        <Surname>Smith</Surname>
      </PersonName>
      <PersonInfo>
        <BirthDate>01-01-1980</BirthDate>
        <MaritalStatus>S</MaritalStatus>
        <Occupation>Manager</Occupation>
      </PersonInfo>
    </Driver>
    <PolicyInfo></PolicyInfo>
  </Policy>
</Info>

期望的结果:

<Info>
  <Principal id="Insured">
    <PersonName>
      <GivenName>Jane</GivenName>
      <OtherGivenName>A</OtherGivenName>
      <Surname>Doe</Surname>
    </PersonName>
    <PersonInfo>
      <BirthDate>01-01-1980</BirthDate>
      <MaritalStatus>M</MaritalStatus>
    </PersonInfo>
    <PrincipalInfo></PrincipalInfo>
  </Principal>
  <Policy>
    <Driver id="Insured">
      <PersonName>
        <GivenName>Jane</GivenName>
        <OtherGivenName>A</OtherGivenName>
        <Surname>Doe</Surname>
      </PersonName>
      <PersonInfo>
        <BirthDate>01-01-1980</BirthDate>
        <MaritalStatus>M</MaritalStatus>
        <Occupation>Manager</Occupation>
      </PersonInfo>
    </Driver>
    <PolicyInfo></PolicyInfo>
  </Policy>
</Info>

1 个答案:

答案 0 :(得分:2)

这是一个完整的解决方案

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

 <xsl:key name="kPrincipalById" match="Principal"
  use="@id"/>

 <xsl:key name="kPrincipalChild" match="Principal/*/*"
  use="concat(../../@id, name())"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="Driver/*">
  <xsl:variable name="vPrincipal"
   select="key('kPrincipalById', ../@id)"/>

   <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select=
     "$vPrincipal/*[name()=name(current())]/*"/>
    <xsl:apply-templates select=
     "*[not(key('kPrincipalChild',
                 concat(../../@id,name())
                 )
            )
        ]"/>
   </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<Info>
    <Principal id="Insured">
        <PersonName>
            <GivenName>Jane</GivenName>
            <OtherGivenName>A</OtherGivenName>
            <Surname>Doe</Surname>
        </PersonName>
        <PersonInfo>
            <BirthDate>01-01-1980</BirthDate>
            <MaritalStatus>M</MaritalStatus>
        </PersonInfo>
        <PrincipalInfo></PrincipalInfo>
    </Principal>
    <Policy>
        <Driver id="Insured">
            <PersonName>
                <GivenName>Jane</GivenName>
                <Surname>Smith</Surname>
            </PersonName>
            <PersonInfo>
                <BirthDate>01-01-1980</BirthDate>
                <MaritalStatus>S</MaritalStatus>
                <Occupation>Manager</Occupation>
            </PersonInfo>
        </Driver>
        <PolicyInfo></PolicyInfo>
    </Policy>
</Info>

产生了想要的正确结果:

<Info>
   <Principal id="Insured">
      <PersonName>
         <GivenName>Jane</GivenName>
         <OtherGivenName>A</OtherGivenName>
         <Surname>Doe</Surname>
      </PersonName>
      <PersonInfo>
         <BirthDate>01-01-1980</BirthDate>
         <MaritalStatus>M</MaritalStatus>
      </PersonInfo>
      <PrincipalInfo/>
   </Principal>
   <Policy>
      <Driver id="Insured">
         <PersonName>
            <GivenName>Jane</GivenName>
            <OtherGivenName>A</OtherGivenName>
            <Surname>Doe</Surname>
         </PersonName>
         <PersonInfo>
            <BirthDate>01-01-1980</BirthDate>
            <MaritalStatus>M</MaritalStatus>
            <Occupation>Manager</Occupation>
         </PersonInfo>
      </Driver>
      <PolicyInfo/>
   </Policy>
</Info>

<强>解释

  1. identity rule /模板按原样复制每个节点。身份规则的使用和覆盖是最基本和最强大的XSLT设计模式。

  2. 只有一个额外的模板会覆盖Driver的儿童元素的身份规则。它复制(并有效地替换Driver的相同名称的孙子元素与Principal的相应的祖父元素。然后它仍然处理(复制)Driver的那些没有相应的子孙元素Principal

  3. 的孙子元素
  4. 为了方便地访问Principal及其子孙 - 通过id和id ++ name(),有两个keys已定义并已使用