XSLT新手,取代价值观

时间:2013-05-02 18:51:43

标签: xml xslt

我的错误,我说清楚了:

我有一个简单的问题,但我是XSLT的新手:

我有2个xml文件:

usa1.xml

<?xml version="1.0" encoding="UTF-8"?>
<country>
  <state name="CA">
    <city name="Sunnyvale" county="Sant Clara">
      <street number="123">
        El Comino Ave.
      </street>
    </city>
    <city name="San Jose" county="Sant Clara">
      <street number="345">
        De Anza Ave.
      </street>
    </city>
    <city name="palo Alto" county="Sant Clara">
      <street number="789">
        Shoreline Ave.
      </street>
    </city>

  </state>
</country>

usa2.xml

<?xml version="1.0" encoding="UTF-8"?>
<country>
  <state name="CA">
    <city name="Sunnyvale" county="Sant Clara">
      <street number="999">
        Homestead Ave.
      </street>
    </city>
    <city name="San Jose" county="Sant Clara">
      <street number="888">
        Airport Ave.
      </street>
    </city>
  </state>
</country>

我想使用XSLT将来自usa1.xml的City Sunnyvale和San Jose的所有值和属性替换为来自usa2.xml的数据。

想法输出为usa4.xml:

usa4.xml

<?xml version="1.0" encoding="UTF-8"?>
<country>
  <state name="CA">
    <city name="Sunnyvale" county="Sant Clara">
      <street number="999">
        Homestead Ave.
      </street>
    </city>
    <city name="San Jose" county="Sant Clara">
      <street number="888">
        Airport Ave.
      </street>
    </city>
    <city name="palo Alto" county="Sant Clara">
      <street number="789">
        Shoreline Ave.
      </street>
    </city>

  </state>
</country>

我该怎么做?

我尝试了以下XSLT,但返回的输出不是我所期望的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="usaxml" select="'usa1.xml'" />
  <xsl:variable name="address" select="document($usaxml)//" />

  <xsl:template match="/">
    <xsl:attribute name="number">
       <xsl:value-of select="$address/@street" />
    </xsl:attribute>
  </xsl:template>

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

1 个答案:

答案 0 :(得分:1)

我会使用一个密钥来表示两个文件之间的交叉引用,但是如果你需要在文档之间使用密钥,那么代码对于XSLT 1.0来说有点复杂:

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

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

<xsl:key name="k1" match="state/city" use="concat(../@name, '|', @name)"/>

<xsl:param name="usaxml" select="'test2013050302.xml'" />
<xsl:variable name="address" select="document($usaxml)" />

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

<xsl:template match="state/city">
  <xsl:copy>
    <xsl:variable name="this" select="."/>
    <xsl:for-each select="$address">
      <xsl:variable name="ref-data" select="key('k1', concat($this/../@name, '|', $this/@name))"/>
      <xsl:choose>
        <xsl:when test="$ref-data">
          <xsl:apply-templates select="$ref-data/@* | $ref-data/node()"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="$this/@* | $this/node()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

所以主要输入是

<?xml version="1.0" encoding="UTF-8"?>
<country>
  <state name="CA">
    <city name="Sunnyvale" county="Sant Clara">
      <street number="123">
        El Comino Ave.
      </street>
    </city>
    <city name="San Jose" county="Sant Clara">
      <street number="345">
        De Anza Ave.
      </street>
    </city>
    <city name="palo Alto" county="Sant Clara">
      <street number="789">
        Shoreline Ave.
      </street>
    </city>

  </state>
</country>

,参数文件test2013050302.xml

<?xml version="1.0" encoding="UTF-8"?>
<country>
  <state name="CA">
    <city name="Sunnyvale" county="Sant Clara">
      <street number="999">
        Homestead Ave.
      </street>
    </city>
    <city name="San Jose" county="Sant Clara">
      <street number="888">
        Airport Ave.
      </street>
    </city>
  </state>
</country>

转化结果是

<country>
  <state name="CA">
    <city name="Sunnyvale" county="Sant Clara">
      <street number="999">
        Homestead Ave.
      </street>
    </city>
    <city name="San Jose" county="Sant Clara">
      <street number="888">
        Airport Ave.
      </street>
    </city>
    <city name="palo Alto" county="Sant Clara">
      <street number="789">
        Shoreline Ave.
      </street>
    </city>

  </state>
</country>

使用XSLT 2.0处理器和XSLT 2.0可以大大简化模板

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

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

<xsl:key name="k1" match="state/city" use="concat(../@name, '|', @name)"/>

<xsl:param name="usaxml" select="'test2013050302.xml'" />
<xsl:variable name="address" select="document($usaxml)" />

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

<xsl:template match="state/city">
  <xsl:copy>
    <xsl:variable name="ref-data" select="key('k1', concat(../@name, '|', @name), $address)"/>
    <xsl:choose>
      <xsl:when test="$ref-data">
        <xsl:apply-templates select="$ref-data/(@*, node())"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>