【问题标题】:XSLT grouping continued - xPath issue?XSLT 分组继续 - xPath 问题?
【发布时间】:2011-03-31 05:35:41
【问题描述】:

Dimitre 早些时候帮了大忙……这有点像第二部分。 :)

我一直在绞尽脑汁,仍然没有看到它。

现在我能够隔离下面 xml 示例的品牌,现在我想隔离给定 $Brand 的所有产品类型,就像我能够隔离所有品牌一样.

xml 示例(许多产品中的一个成员)...

<Product>
        <Brand>Brand</Brand>
        <Type>Product Type (Category)</Type>
        ...
    </Product>

这是我能够想出的 xsl。我认为我的错误出现在 xsl:key 的 xPath 表达式中...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="Brand" select="Brand"/> 
  <xsl:output method="html" encoding="utf-8"/>
    <xsl:key name="kProdByType" 
          match="Products/Product/Brand[. = $Brand]" use="../Type"/>

  <xsl:template match="Products">
    <xsl:for-each 
          select="Product[generate-id() = 
          generate-id(key('kProdByType', Type)[1])]
         "><xsl:sort select="Type" /><xsl:value-of 
            select="Type" />|</xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

再次感谢!

【问题讨论】:

    标签: xml xslt xpath xslt-grouping


    【解决方案1】:

    现在您按BrandType 分组。密钥必须是:

    <xsl:key name="kProdByBrandAndType" 
             match="Product" use="concat(Brand,'+++',Type)"/> 
    

    现在,分组:

    <xsl:for-each  
              select="Product[generate-id() =  
                              generate-id(key('kProdByBrandAndType',
                                              concat($Brand,'+++',Type))[1])]">
    

    在模式中使用变量/参数应该是错误的,但我认为至少 MSXSL 不会在键中抱怨这一点。 为了安全,请勿使用

    <xsl:key name="kProdByType" match="Product[Brand=$Brand]" use="Type"/> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多