使用第二个xml文件更新子节点值

时间:2017-05-04 11:04:23

标签: xml xslt

我想更新子节点的值" dateEta"从第二个xml文件。 "识别"两个文件都是共同的。

1.xml

<defac>
<fac>
  <identification>170610001-01</identification>
  <order>
   <test1>test</test1>  
   <dateEta>2017-02-03</dateEta>
   <test2>test</test2>
  </order>
</fac>
<fac>
  <identification >170610002-01</identification>
  <order>
   <test1>test</test1>  
   <dateEta>2017-02-03</dateEta>
   <test2>test</test2>
  </order>
</fac>
<fac>
  <identification>170610003-01</identification>
  <order>
   <test1>test</test1>  
   <dateEta>2017-02-03</dateEta>
   <test2>test</test2>
  </order>
</fac>
</defac>

2.XML

<defac>
<fac>
  <identification>170610001-01</identification>
  <order>
  <dateEta>2017-02-05</dateEta>
  </order>
</fac>
<fac>
  <identification >170610002-01</identification>
  <order>
  <dateEta>2017-01-09</dateEta>
  </order>
</fac>
<fac>
  <identification>170610003-01</identification>
  <order>
  <dateEta>2017-02-08</dateEta>
  </order>
</fac>
</defac>

我想要的 - &gt; update.xml

<defac>
    <fac>
      <identification>170610001-01</identification>
      <order>
       <test1>test</test1>  
       <dateEta>2017-02-05</dateEta>
       <test2>test</test2>
      </order>
    </fac>
    <fac>
      <identification >170610002-01</identification>
      <order>
       <test1>test</test1>  
       <dateEta>2017-01-09</dateEta>
       <test2>test</test2>
      </order>
    </fac>
    <fac>
      <identification>170610003-01</identification>
      <order>
       <test1>test</test1>  
       <dateEta>2017-02-08</dateEta>
       <test2>test</test2>
      </order>
    </fac>
    </defac>

我尝试了什么

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

    <xsl:param name="data-uri" select="'2.xml'"/>
    <xsl:param name="data-doc" select="document($data-uri)"/>

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

    <xsl:template match="dateEta">
        <xsl:copy>
            <xsl:variable name="match" select="$data-doc//fac[identification = current()/../identification]/order/dateEta"/>
            <xsl:choose>
                <xsl:when test="$match">
                    <xsl:value-of select="$match"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>     
    </xsl:template>
</xsl:stylesheet>

但那不起作用。日期根本没有更新。如果&#34; dateEta&#34;与#34;识别&#34;处于同一水平我可以让它发挥作用,但当它处于较低的水平时,没有任何事情发生。

你知道我在哪里犯了错误吗?

谢谢

1 个答案:

答案 0 :(得分:2)

您需要更改:

<xsl:variable name="match" select="$data-doc//fac[identification = current()/../identification]/order/dateEta"/>

为:

<xsl:variable name="match" select="$data-doc//fac[identification = current()/../../identification]/order/dateEta"/>

或者:

<xsl:variable name="match" select="$data-doc//fac[identification = current()/ancestor::fac/identification]/order/dateEta"/>

因为dateEta不是identification的兄弟;其父order是。