XSLT让孙节点模板

XSLT让孙节点模板

问题描述:

属性值

鉴于这样的XML:XSLT让孙节点模板

<?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> 
... 

谢谢。

您正在使用错误的xpath选项。用途:

select="Identifiers/Identifier[@Name='UPC']/@Value" 

如果你只打算与这两个节点的值与模板匹配他们:

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

尼斯。在这种情况下,我实际上需要很多其他的东西,但是我会将它们存档以供将来使用。谢谢! – Mike 2013-03-27 11:57:11