簡體   English   中英

將xpath與具有不同值的xslt一起使用時出現問題

[英]Problem in using xpath with xslt having distinct values

我有類似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

但是我沒有得到期望的結果...不同的值工作正常..當我添加它和克勞斯時出現問題

您需要對節點集中的所有節點進行排序-它們根本不是兄弟姐妹

此轉換:

<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,

您將需要另一遍來按所需順序對它們進行排序,並消除尾隨的逗號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM