使用xslt更新XML文件

使用xslt更新XML文件,xml,xslt,Xml,Xslt,我有两个xml文件 文件1- <?xml version="1.0" encoding="UTF-8"?> <Root> <School> <section id="12" name="Apple"/> <section id="50" name="Newton"/> </School> <Students&g

我有两个xml文件

文件1-

   <?xml version="1.0" encoding="UTF-8"?>
    <Root>
        <School>
            <section id="12" name="Apple"/>
            <section id="50" name="Newton"/>
        </School>
        <Students>
            <roll no="111" name="Smith"/>
            <roll no="122" name="Alan"/>
            <roll no="20" name="Bruce"/>
        </Students>
        <Teachers>
            <Math>
                <emp id="55" name="Karen"/>
                <emp id="2" name="David"/>
            </Math>
            <Science>
                <emp id="1" name="Thomas"/>
            </Science>
        </Teachers>
        <Sports>
            <Indoor>
                <Boardgame>
                    <game id="12" name="Chess"/>
                </Boardgame>
                <Arcade>
                    <game id="3" name="Car Racing"/>
                </Arcade>
            </Indoor>
            <Outdoor>
                <Field>
                    <game id="1" name="Football"/>
                    <game id="100" name="Cricket"/>
                </Field>
                <Court>
                    <game id="2" name="Tennis"/>
                </Court>
            </Outdoor>
        </Sports>
    </Root>

文件2-

<?xml version="1.0" encoding="UTF-8"?>
<Updates>
    <School>
        <section id="12" name="Orange"/>
    </School>
    <Students>
        <roll no="122" name="Sam"/>
    </Students>
    <Teachers>
        <Math>
            <emp id="300" name="Steve" />
        </Math>
    </Teachers>
    <Sports>
        <Indoor>
            <Boardgame>
                <game id="37" name="Monopoly"/>
            </Boardgame>
            <Boardgame2>
                <game id="36" name="Ludo"/>
            </Boardgame2>
        </Indoor>
        <Outdoor>
            <Field>
                <game id="1" name="Football"/>
                <game id="100" name="Bull Fighting"/>
            </Field>
            <Court>
                <game id="19" name="Badminton"/>
            </Court>
        </Outdoor>
        <Computer>
            <game id="10" name="AOE" />
        </Computer>
    </Sports>
</Updates>

我需要合并这些文件,以便获得以下输出。如果id/no匹配,file2中的条目将覆盖file1中的条目。将根据需要从file2在适当的层次结构下的输出中添加新元素

变换输出-

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <School>
        <section id="12" name="Orange"/>
        <section id="50" name="Newton"/>
    </School>
    <Students>
        <roll no="111" name="Smith"/>
        <roll no="122" name="Sam"/>
        <roll no="20" name="Bruce"/>
    </Students>
    <Teachers>
        <Math>
            <emp id="55" name="Karen"/>
            <emp id="2" name="David"/>
            <emp id="300" name="Steve" />
        </Math>
        <Science>
            <emp id="1" name="Thomas"/>
        </Science>
    </Teachers>
    <Sports>
        <Indoor>
            <Boardgame>
                <game id="12" name="Chess"/>
                <game id="37" name="Monopoly"/>
            </Boardgame>
            <Arcade>
                <game id="3" name="Car Racing"/>
            </Arcade>
            <Boardgame2>
                <game id="36" name="Ludo"/>
            </Boardgame2>
        </Indoor>
        <Outdoor>
            <Field>
                <game id="1" name="Football"/>
                <game id="100" name="Bull Fighting"/>
            </Field>
            <Court>
                <game id="2" name="Tennis"/>
                <game id="19" name="Badminton"/>
            </Court>
        </Outdoor>
        <Computer>
            <game id="10" name="AOE" />
        </Computer>     
    </Sports>
</Root>

下面是XSLT,但它只适用于更新,不适用于插入

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="no"/>

    <xsl:template match="/">
        <xsl:apply-templates select="node()">
            <xsl:with-param name="doc-context" select="document('file2.xml')/node()" />
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="node()">
        <xsl:param name="doc-context" />

        <xsl:variable name="id" select="@id" />
        <xsl:variable name="no" select="@no" />

        <xsl:copy>
            <xsl:copy-of select="@*|$doc-context[@id = $id or @no = $no]/@*" />
            <xsl:apply-templates select="node()">
                <xsl:with-param name="doc-context" select="$doc-context/node()" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

稍后我将编写完整的样式表,但现在,我将使用以下方法来解决此问题

  • 使用XSLT2.0或3.0
  • 从基本的身份转换开始
  • 使用空模板,删除@id与更新文件中任何@id值匹配的元素(稍后我们将了解如何测试)
  • “可识别元素的父元素”的模板,即学校、数学等。如何执行此操作取决于此元素名称列表是固定的还是动态的
  • 在前面提到的模板中,从正常处理开始(子节点上的xsl:copy和xsl:apply模板),还可以从更新文件中添加(在xsl:copy下)与焦点节点路径匹配的元素
  • 对于步骤2和步骤5中的测试,可以使用xsl:key和key()函数。但是要注意新手的一个常见陷阱:2-arity key()函数有一个焦点文档的隐式参数


    更新 那么

    <xsl:transform
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:so="http://stackoverflow.com/questions/34522017"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        version="2.0"
        exclude-result-prefixes="so xs">
    
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:param name="updates-file" as="xs:string" />
    <xsl:strip-space elements="*" />
    
    <xsl:variable name="updates" select="doc($updates-file)" />
    
    <xsl:function name="so:merge-key" as="xs:string">
      <xsl:param name="ele" as="element()" />
      <!-- Updates and Root are at the same for merging purposes. -->
      <xsl:variable name="ele-name" select="local-name($ele[not(self::Updates)][not(self::Root)])" />
      <xsl:value-of select="concat( $ele-name, '!', $ele/@id, $ele/@no)" /> 
    </xsl:function>
    
    <xsl:template match="@*|comment()|processing-instruction()|text()">
      <xsl:copy />
    </xsl:template>
    
    <xsl:template match="*">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="/Root">
      <xsl:apply-templates select="." mode="update">
          <xsl:with-param name="peer-updates" select="$updates/Updates" />
      </xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="*[not(@id|@no)]" mode="update">
      <xsl:param name="peer-updates" as="element()*" />
      <xsl:variable name="this-key" select="so:merge-key(.)" />
      <xsl:variable name="compare-set" select="*" as="element()*" />
      <xsl:variable name="merge-other" select="$peer-updates[so:merge-key(.) eq $this-key]/*" as="element()*" />
      <xsl:copy>
        <!-- Process the fluff. -->
        <xsl:apply-templates select="@*|comment()|processing-instruction()|text()" />
    
        <!-- Now the unchanged orginal elements. -->
        <xsl:apply-templates select="*[not( so:merge-key(.) = $merge-other/so:merge-key(.))]" />
    
        <!-- Now the updated elements. -->
        <xsl:apply-templates select="*[so:merge-key(.) = $merge-other/so:merge-key(.)]" mode="update">
          <xsl:with-param name="peer-updates" select="$merge-other[so:merge-key(.) = $compare-set/so:merge-key(.)]" />
        </xsl:apply-templates>
    
        <!-- Now new elements. -->
        <xsl:apply-templates select="$merge-other[ not( so:merge-key(.) = $compare-set/so:merge-key(.))]" />
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*[@id|@no]" mode="update">
      <xsl:param name="peer-updates" as="element()*" />
      <xsl:variable name="this-key" select="so:merge-key(.)" />
      <xsl:variable name="merge-other" select="$peer-updates[so:merge-key(.) eq $this-key]" as="element()?" />
      <xsl:copy-of select="if ($merge-other) then $merge-other else ." />
    </xsl:template>
    
    </xsl:transform>
    

    一般来说,
    xsl:for-each
    都是丑陋和糟糕的。这是一种xslt反模式。如果这是我的产品代码,我有更多的时间来考虑它,我会使用模板匹配机制。但不管怎样,这里有一个快速而肮脏的解决方案。

    到目前为止,您尝试过什么吗?如果您尝试解决您的问题,并且发布了您的尝试代码,我们可以提供帮助。感谢您包含了我迄今为止一直在使用的XSLT,该XSLT适用于基于ID或编号的更新。但是它不处理插入或删除。您能用XSLT 2.0吗?3.0?另外,订购是否重要?更改文件如何表示删除?谢谢Sean,这很有效!但正如您在前面的问题中所指出的,订单确实受到了影响。在一个大的xml文件中,顺序变得混乱。是否有办法保持相同的顺序?您的顺序规则是什么?顺序应保持不变,如文件1所示。新添加的节点可以添加为最后一个同级节点。
    <!-- For the original elements, both unchanged and to be updated. -->
    <xsl:for-each select="*">
      <xsl:choose>
        <xsl:when test="so:merge-key(.) = $merge-other/so:merge-key(.)">
          <xsl:apply-templates select="." mode="update">
            <xsl:with-param name="peer-updates" select="$merge-other[so:merge-key(.) = $compare-set/so:merge-key(.)]" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="." />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>