1

首先,我不喜欢 XSLT。其次,我知道有几篇关于如何使用 XSLT 执行合并的文章,但我没有找到任何关于我的特定挑战的内容。

我有 2 个 XML 文件。一个是新的客户信息,另一个是下面的当前/以前的信息。我需要生成的 XML 来合并所有客户/地址并将属性(NoChange、Updated、Deleted、New)添加到最终 XML 的属性中:


输入 1

当前客户信息。

<Customer>
 <CustId>1</CustId>
 <CustName>Acme</CustName>
 <Addresses>
  <Address>
   <AddressesId>1</AddressesId>
   <Street>123 Main</Street>
  </Address>
  <Address>
   <AddressesId>2</AddressesId>
   <Street>345 Main</Street</Street>
  </Address>
  <Address>
   <AddressesId>4</AddressesId>
   <Street>888 Goner St.</Street>
  </Address>
 </Addresses>
</Customer>

输入 2

更新信息。

<Customer>
 <CustId>1</CustId>
 <CustName>Acme</CustName>
 <Addresses>
  <Address>
   <AddressesId>2</AddressesId>
   <Street>999 Updated St.</Street>
  </Address>
  <Address>
   <AddressesId>3</AddressesId>
   <Street>3999 New St.</Street>
  </Address>
 </Addresses>
</Customer>

结果

<Customer>
 <CustId>1</CustId>
 <CustName>Acme</CustName>
 <Addresses>
  <Address>
   <Address status="NoChange">
   <AddressesId>1</AddressesId>
   <Street>123 Main</Street>
  </Address>
  <Address>
   <Address status="Updated">
   <AddressesId>2</AddressesId>
   <Street>999 Updated St.</Street>
  </Address>
   <Address status="New">
   <AddressesId>3</AddressesId>
   <Street>3999 New St.</Street>
  </Address>
  <Address status="Deleted">
   <AddressesId>4</AddressesId>
   <Street>888 Goner St.</Street>
  </Address>
 </Addresses>
</Customer>

我怎样才能做我想要的合并?

4

2 回答 2

1

我决定尝试将此作为 XSLT 3.0 中新 xsl:merge 指令的用例。使用当前的 Saxon 实现,以下给出了预期的结果:

<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:variable name="original" select="doc('merge018-current.xml')"/>
  <xsl:variable name="updates" select="doc('merge018-updates.xml')"/>

  <xsl:template name="main">
    <xsl:apply-templates select="$original"/>
  </xsl:template>  

  <xsl:template match="Addresses">
    <Addresses>
      <xsl:merge>
        <xsl:merge-source for-each="$updates, $original"
                        select=".//Address">
          <xsl:merge-key select="AddressesId"/>
        </xsl:merge-source>
        <xsl:merge-action>
          <xsl:variable name="status" select="
            if (count(current-group()) = 1)
            then if (current-group()[1]/root(.) is $original) then 'Deleted' else 'New'
            else if (deep-equal(current-group()[1], current-group()[2])) then 'NoChange' else 'Updated'"/>
          <Address status="{$status}">
            <xsl:copy-of select="current-group()[1]/(AddressesId, Street)"/>
          </Address>
        </xsl:merge-action>
      </xsl:merge>
    </Addresses>
  </xsl:template>
</xsl:stylesheet>

我并不是建议将此作为一种实用的解决方案,只是为了您的兴趣而提供它。如果您对此或类似的东西作为测试用例发布有任何异议,请现在说出来。

于 2012-04-23T09:56:48.760 回答
0

I. 这个 XSLT 1.0 转换(相应的 XSLT 2.0 解决方案更短更容易):

<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="kAddrById" match="Address"
  use="concat(../../CustId, '+', ../../CustName,
              '+', AddressesId)"/>

 <xsl:key name="kNodeByGenId" match="node()"
          use="generate-id()"/>

 <xsl:param name="pUpdatesPath" select=
  "'file:///c:/temp/delete/CustomersUpdates.xml'"/>

 <xsl:variable name="vUpdates" select=
   "document($pUpdatesPath)"/>

 <xsl:variable name="vmainDoc" select="/"/>

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

 <xsl:template match="Addresses">
  <Addresses>
   <xsl:apply-templates select="Address | $vUpdates/*/*/*">
    <xsl:sort select="AddressesId" data-type="number"/>
   </xsl:apply-templates>
  </Addresses>
 </xsl:template>

 <xsl:template match="Address">
   <xsl:variable name="vIsThisUpdate" select=
     "generate-id(/) = generate-id($vUpdates)"/>

   <xsl:variable name="vOtherDoc" select=
    "$vmainDoc[$vIsThisUpdate]
    |
     $vUpdates[not($vIsThisUpdate)]
    "/>

    <xsl:variable name="vCustId" select="../../CustId"/>
    <xsl:variable name="vCustName" select="../../CustName"/>
    <xsl:variable name="vAddrId" select="AddressesId"/>

    <xsl:variable name="vOtherNodeId">
     <xsl:for-each select="$vOtherDoc">
       <xsl:value-of select=
       "generate-id(key('kAddrById',
                        concat($vCustId,'+', $vCustName,
                               '+', $vAddrId)
                       )
                   )"/>
     </xsl:for-each>
    </xsl:variable>
    <xsl:apply-templates mode="selected"
     select="self::node()
              [$vIsThisUpdate
             or
              (not($vIsThisUpdate) and not(string($vOtherNodeId)))
              ]">
     <xsl:with-param name="pIsUpdating" select="$vIsThisUpdate"/>
     <xsl:with-param name="pOtherDoc" select="$vOtherDoc"/>
     <xsl:with-param name="pOtherNodeId"
                     select="string($vOtherNodeId)"/>
    </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="Address" mode="selected">
  <xsl:param name="pIsUpdating"/>
  <xsl:param name="pOtherDoc" select="/.."/>
  <xsl:param name="pOtherNodeId"/>

  <xsl:variable name="vStatus">
   <xsl:choose>
     <xsl:when test="$pIsUpdating and not($pOtherNodeId)">New</xsl:when>
       <xsl:when test="$pIsUpdating">
         <xsl:variable name="vOldStreet">
          <xsl:for-each select="$pOtherDoc">
            <xsl:value-of select=
              "key('kNodeByGenId', $pOtherNodeId)/Street"/>
          </xsl:for-each>
         </xsl:variable>

         <xsl:choose>
           <xsl:when test=
           "Street = string($vOldStreet)">NoChange</xsl:when>
           <xsl:otherwise>Updated</xsl:otherwise>
         </xsl:choose>
       </xsl:when>
       <xsl:otherwise>Deleted</xsl:otherwise>
   </xsl:choose>
  </xsl:variable>

  <Address>
    <Address status="{$vStatus}"/>
    <xsl:apply-templates/>
  </Address>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Customer>
    <CustId>1</CustId>
    <CustName>Acme</CustName>
    <Addresses>
        <Address>
            <AddressesId>1</AddressesId>
            <Street>123 Main</Street>
        </Address>
        <Address>
            <AddressesId>2</AddressesId>
            <Street>345 Main</Street>
        </Address>
        <Address>
            <AddressesId>4</AddressesId>
            <Street>888 Goner St.</Street>
        </Address>
    </Addresses>
</Customer>

并在c:/temp/delete/CustomersUpdates.xml此 XML 文档中(与提供的略有不同,以使第一个地址以状态“NoChange”结束):

<Customer>
    <CustId>1</CustId>
    <CustName>Acme</CustName>
    <Addresses>
        <Address>
            <AddressesId>1</AddressesId>
            <Street>123 Main</Street>
        </Address>
        <Address>
            <AddressesId>2</AddressesId>
            <Street>999 Updated St.</Street>
        </Address>
        <Address>
            <AddressesId>3</AddressesId>
            <Street>3999 New St.</Street>
        </Address>
    </Addresses>
</Customer>

产生想要的正确结果

<Customer>
   <CustId>1</CustId>
   <CustName>Acme</CustName>
   <Addresses>
      <Address>
         <Address status="NoChange"/>
         <AddressesId>1</AddressesId>
         <Street>123 Main</Street>
      </Address>
      <Address>
         <Address status="Updated"/>
         <AddressesId>2</AddressesId>
         <Street>999 Updated St.</Street>
      </Address>
      <Address>
         <Address status="New"/>
         <AddressesId>3</AddressesId>
         <Street>3999 New St.</Street>
      </Address>
      <Address>
         <Address status="Deleted"/>
         <AddressesId>4</AddressesId>
         <Street>888 Goner St.</Street>
      </Address>
   </Addresses>
</Customer>

二、XSLT 2.0 解决方案

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

 <xsl:key name="kAddrById" match="Address"
  use="concat(../../CustId, '+', ../../CustName,
              '+', AddressesId)"/>

 <xsl:param name="pUpdatesPath" select=
  "'file:///c:/temp/delete/CustomersUpdates.xml'"/>

 <xsl:variable name="vUpdates" select=
   "document($pUpdatesPath)"/>

 <xsl:variable name="vmainDoc" select="/"/>

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

 <xsl:template match="Addresses">
  <Addresses>
   <xsl:apply-templates select="Address | $vUpdates/*/*/*">
    <xsl:sort select="AddressesId" data-type="number"/>
   </xsl:apply-templates>
  </Addresses>
 </xsl:template>

 <xsl:template match="Address[root() is $vUpdates]">
    <xsl:variable name="vOtherDoc" select="$vmainDoc"/>

    <xsl:variable name="vOtherNode" select="my:OtherNode(., $vOtherDoc)"/>

    <xsl:variable name="vStatus" select=
     "concat('New'[not($vOtherNode)],
             'NoChange'[$vOtherNode 
                       and current()/Street eq $vOtherNode/Street],
             'Updated'[$vOtherNode and current()/Street ne $vOtherNode/Street]
             )"/>
    <xsl:apply-templates select="self::node()" mode="selected">
     <xsl:with-param name="pStatus" select="$vStatus"/>
    </xsl:apply-templates>
 </xsl:template>

  <xsl:template match="Address[not(root() is $vUpdates)]">
   <xsl:variable name="vOtherDoc" select="$vUpdates"/>

    <xsl:variable name="vOtherNode" select="my:OtherNode(., $vOtherDoc)"/>

    <xsl:apply-templates select="self::node()[not($vOtherNode)]" mode="selected">
      <xsl:with-param name="pStatus" select="'Deleted'"/>
    </xsl:apply-templates>
  </xsl:template>

 <xsl:template match="Address" mode="selected">
  <xsl:param name="pStatus"/>

  <Address>
    <Address status="{$pStatus}"/>
    <xsl:apply-templates/>
  </Address>
 </xsl:template>

 <xsl:function name="my:OtherNode" as="element()?">
   <xsl:param name="pThis" as="element()"/>
   <xsl:param name="pOtherDoc" as="document-node()"/>

    <xsl:sequence select=
     "key('kAddrById',
          concat($pThis/../../CustId,'+', $pThis/../../CustName,
                 '+', $pThis/AddressesId
                 ),
                 $pOtherDoc
          )"/>
 </xsl:function>
</xsl:stylesheet>

当此转换应用于相同的文档时,会产生相同的正确结果。

于 2012-04-22T22:00:30.197 回答