0

我的错误,我说清楚:

我有一个简单的问题,但我是 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 将 City Sunnyvale 和 San Jose 的所有值和属性替换为usa1.xml来自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>
4

1 回答 1

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>
于 2013-05-03T09:37:22.970 回答