0

给定这样的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Products>
  <Product someId="1EFAD9659EC">
    <Identifiers>
      <Identifier Id="234532423" Name="globalTradeItemNumber (GTIN)" Value="00671657621322" />
      <Identifier Id="99845898" Name="Internal Supplier Part #" Value="DEL 20 10B015000" />
      <Identifier Id="49348598" Name="MFG Model # (Series)" Value="DEL 20 10B015000" />
      <Identifier Id="439854985" Name="MFG Part # (OEM)" Value="DEL 20 10B015000" />
      <Identifier Id="2349832489" Name="UPC" Value="671657621322" />
    </Identifiers>
  </Product>    
  <Product someId="1EFAD9659EC">
    <Identifiers>
      <Identifier Id="234532423" Name="globalTradeItemNumber (GTIN)" Value="51651518" />
      <Identifier Id="99845898" Name="Internal Supplier Part #" Value="TIM 20 10B015000" />
      <Identifier Id="49348598" Name="MFG Model # (Series)" Value="TOM 20 10B015000" />
      <Identifier Id="439854985" Name="MFG Part # (OEM)" Value="TAK 20 10B015000" />
      <Identifier Id="2349832489" Name="UPC" Value="87468387468" />
    </Identifiers>
  </Product>    
      . . .

我想最终得到类似的东西

...
  <Product upc="671657621322"/>
  <Product upc="87468387468"/>
...

但我得到的是

...
  <Product upc="true"/>
  <Product upc="true"/>
...

我不断得到我选择的布尔答案,而不是属性的值。我在这里做错了什么愚蠢的事情?这是我正在尝试的 XSLT:

...
    <xsl:template match="/">
        <Output>
            <xsl:apply-templates />
        </Output>
    </xsl:template>

    <xsl:template match="Product">
        <xsl:variable name="productCode" select="./Identifiers/Identifier/@Name='UPC'"/>
        <Product upc="{$productCode}">
        </Product>
    </xsl:template>
...

谢谢。

4

2 回答 2

2

您使用了错误的 xpath 选择。利用:

select="Identifiers/Identifier[@Name='UPC']/@Value"
于 2013-03-26T20:33:41.590 回答
0

如果您只对这两个节点值感兴趣,请将它们与模板匹配:

<xsl:template match="Product/Identifiers/Identifier[@Name='UPC']">
    <xsl:variable name="productCode" select="@Value"/>
    <Product upc="{$productCode}">
    </Product>
</xsl:template>
于 2013-03-26T20:36:15.720 回答