使用xsl.both打开两个用于查找的xml文档具有相同的元素

时间:2013-02-06 04:57:38

标签: xml xslt lookup

我正在使用xsl进行XML转换,我必须使用多次查找。查找xml的格式如下。

<lookups>
    <lookup attr1="val1a" attr2="val1b">
    <lookup attr1="val2a" attr2="val2b">
<lookups>

所有查找xmls都使用以上格式。例如,元素A的查找文件就在那里,元素B的另一个查找文件也有相同的元素结构,但是与元素B相关的值不同。然后我在主xsl文件中进行如下查找。

<!-- here come the first lookup for element A -->
<xsl:key name="A-lookup" match="lookup" use="@attr1"/>
<xsl:variable name="ALookupDoc" select="document('ALookup.xml')"/>

<xsl:template match="A">
    <Anew>
        <xsl:apply-templates select="$ALookupDoc/*">
        <xsl:with-param name="curr-code" select="string(.)"/>
    </xsl:apply-templates>
    </Anew>
</xsl:template>

<xsl:template match="lookups">
    <xsl:param name="curr-code"/>
    <xsl:value-of select="key('A-lookup', $curr-code)/@attr2"/>
</xsl:template>

<!-- And the second one for element B -->
<xsl:key name="B-lookup" match="lookup" use="@attr1"/>
<xsl:variable name="BLookupDoc" select="document('BLookup.xml')"/>

<xsl:template match="B">
    <Anew>
        <xsl:apply-templates select="$BLookupDoc/*">
        <xsl:with-param name="curr-code" select="string(.)"/>
    </xsl:apply-templates>
    </Anew>
</xsl:template>

<xsl:template match="lookups">
    <xsl:param name="curr-code"/>
    <xsl:value-of select="key('B-lookup', $curr-code)/@attr2"/>
</xsl:template>

现在好了。问题是首先查找元素A的工作正常。但不是第二个。我看到有两个有问题的部分。

  1. 两个查找都具有相同的元素名称(查找和查找)。一旦我将第二个查找XML更改为不同的元素(lookupsX和lookupX)和模板,它们就可以正常工作。
  2. 同一元素有两个模板(元素A和B的查找)
  3. 我仍然不确定问题究竟在哪里。希望有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

我认为问题在于您有两个模板具有完全相同的match值,因此实际上只使用其中一个模板。您可以通过为它们提供不同的模式来解决这个问题,但是如何做到这一点:

  <!-- here come the first lookup for element A -->
  <xsl:key name="A-lookup" match="lookup" use="@attr1"/>
  <xsl:variable name="ALookupDoc" select="document('ALookup.xml')"/>

  <xsl:template match="A">
    <Anew>
      <xsl:apply-templates select="$ALookupDoc/*">
        <xsl:with-param name="curr-code" select="string(.)"/>
        <xsl:with-param name="keyName" select="'A-lookup'" />
      </xsl:apply-templates>
    </Anew>
  </xsl:template>

  <!-- And the second one for element B -->
  <xsl:key name="B-lookup" match="lookup" use="@attr1"/>
  <xsl:variable name="BLookupDoc" select="document('BLookup.xml')"/>

  <xsl:template match="B">
    <Anew>
      <xsl:apply-templates select="$BLookupDoc/*">
        <xsl:with-param name="curr-code" select="string(.)"/>
        <xsl:with-param name="keyName" select="'B-lookup'" />
      </xsl:apply-templates>
    </Anew>
  </xsl:template>

  <xsl:template match="lookups">
    <xsl:param name="curr-code"/>
    <xsl:param name="keyName" />

    <xsl:value-of select="key($keyName, $curr-code)/@attr2"/>
  </xsl:template>

然后,您可能不需要两个键,因为它们都具有相同的定义。请看看这是否有效:

  <!-- here come the first lookup for element A -->
  <xsl:key name="lookup" match="lookup" use="@attr1"/>
  <xsl:variable name="ALookupDoc" select="document('ALookup.xml')"/>

  <xsl:template match="A">
    <Anew>
      <xsl:apply-templates select="$ALookupDoc/*">
        <xsl:with-param name="curr-code" select="string(.)"/>
      </xsl:apply-templates>
    </Anew>
  </xsl:template>

  <!-- And the second one for element B -->
  <xsl:variable name="BLookupDoc" select="document('BLookup.xml')"/>

  <xsl:template match="B">
    <Anew>
      <xsl:apply-templates select="$BLookupDoc/*">
        <xsl:with-param name="curr-code" select="string(.)"/>
      </xsl:apply-templates>
    </Anew>
  </xsl:template>

  <xsl:template match="lookups">
    <xsl:param name="curr-code"/>

    <xsl:value-of select="key('lookup', $curr-code)/@attr2"/>
  </xsl:template>