使用xpath与xslt具有不同值的问题

时间:2010-03-31 22:10:21

标签: xml xslt xpath

我喜欢XML,

<items> 
  <item> 
    <products> 
      <product>laptop</product> 
      <product>charger</product> 
      <product>Cam</product>
    </products> 
  </item> 
  <item> 
    <products> 
      <product>laptop</product> 
      <product>headphones</product>  
      <product>Photoframe</product>  
    </products>   
  </item> 
  <item> 
    <products> 
      <product>laptop</product> 
      <product>charger</product>
      <product>Battery</product>   
    </products>   
  </item> 
</items> 

我正在使用xslt

//无法将xpath更改为从其他地方获取

<xsl:param name="xparameter" select="items/item[products/product='laptop' and products/product='charger']"></xsl:param>

    <xsl:template match="/">

        <xsl:for-each select="$xparameter">

          <xsl:for-each select="products/product[not(.=preceding::product)]">
              <xsl:sort select="."></xsl:sort>
              <xsl:value-of select="." ></xsl:value-of>,
          </xsl:for-each>

          </xsl:for-each>

    </xsl:template>

我希望输出为

laptop
charger
cam
Battery

但是我没有得到结果,因为我期待......不同的值工作得很好..当我添加那个和克劳斯时,有些事情出错了

1 个答案:

答案 0 :(得分:0)

您需要对节点集中的所有节点进行排序 - 它们根本不是兄弟节点

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:param name="xparameter" select="items/item[products/product='laptop' and products/product='charger']"></xsl:param>

  <xsl:template match="/">
    <xsl:for-each select="$xparameter/products/product">
     <xsl:variable name="vPos" select="position()"/>
     <xsl:if test="not(. = ($xparameter/products/product)[position() > $vPos])">
      <xsl:value-of select="concat(., ',')"/>
     </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时,会生成不同的值列表

Cam,laptop,charger,Battery,

你需要另一个传递来按照想要的顺序对它们进行排序并消除尾随的逗号

相关问题