2011-09-27 78 views
0

我有一个XML文档(如下图所示),我需要做的就是让所有的独特类型@ID值的列表,无论在哪里,他们都低于SizeRangesXSLT帮助获得不同的值

因此,对于这个我需要把10,8,6,5放到我可以使用的东西上。

最终,我试图显示每个SizeRange在一个表中彼此相邻,并获得相同的行排列相同的类型。但是我遇到的问题是提取一个可以循环的类型的清单。

XML文档:

<SizeRanges> 
    <SizeRange ID="1" Name="8-18"> 
     <Types> 
      <Type ID="10"> 
       <Size Name="8" Quantity="1" /> 
       <Size Name="10" Quantity="2" /> 
       <Size Name="12" Quantity="2" /> 
       <Size Name="14" Quantity="3" /> 
       <Size Name="16" Quantity="1" /> 
       <Size Name="18" Quantity="1" /> 
      </Type> 
      <Type ID="8"> 
       <Size Name="8" Quantity="1" /> 
       <Size Name="10" Quantity="1" /> 
       <Size Name="12" Quantity="2" /> 
       <Size Name="14" Quantity="2" /> 
       <Size Name="16" Quantity="1" /> 
       <Size Name="18" Quantity="1" /> 
      </Type> 
      <Type ID="6"> 
       <Size Name="8" Quantity="1" /> 
       <Size Name="10" Quantity="1" /> 
       <Size Name="12" Quantity="1" /> 
       <Size Name="14" Quantity="1" /> 
       <Size Name="16" Quantity="1" /> 
       <Size Name="18" Quantity="1" /> 
      </Type> 
      <Type ID="5"> 
       <Size Name="10" Quantity="1" /> 
       <Size Name="12" Quantity="1" /> 
       <Size Name="14" Quantity="1" /> 
       <Size Name="16" Quantity="1" /> 
       <Size Name="18" Quantity="1" /> 
      </Type> 
     </Types> 
    </SizeRange> 
    <SizeRange ID="2" Name="S-XL"> 
     <Types> 
      <Type ID="10"> 
       <Size Name="S" Quantity="1" /> 
       <Size Name="M" Quantity="3" /> 
       <Size Name="L" Quantity="4" /> 
       <Size Name="XL" Quantity="2" /> 
      </Type> 
      <Type ID="8"> 
       <Size Name="S" Quantity="1" /> 
       <Size Name="M" Quantity="2" /> 
       <Size Name="L" Quantity="3" /> 
       <Size Name="XL" Quantity="2" /> 
      </Type> 
      <Type ID="6"> 
       <Size Name="S" Quantity="1" /> 
       <Size Name="M" Quantity="2" /> 
       <Size Name="L" Quantity="2" /> 
       <Size Name="XL" Quantity="1" /> 
      </Type> 
      <Type ID="5"> 
       <Size Name="S" Quantity="1" /> 
       <Size Name="M" Quantity="1" /> 
       <Size Name="L" Quantity="2" /> 
       <Size Name="XL" Quantity="1" /> 
      </Type> 
     </Types> 
    </SizeRange> 
</SizeRanges> 

回答

2

给这一个镜头:

<xsl:key name="types" match="Type" use="@ID"/> 
<xsl:variable name="distinctTypes" as="xs:integer*" select="distinct-values(//SizeRange/Types/Type)" /> 
<xsl:template match="/"> 
    <xsl:for-each select="$distinctTypes"> 
     <xsl:value-of select="." /> 
    </xsl:for-each> 
</xsl:template> 

这里的另一种方式:

<xsl:key name="types" match="Type" use="@ID"/> 
<xsl:template match="/"> 
    <xsl:for-each select="//SizeRange/Types/Type[generate-id() = generate-id(key('types', @ID)[1])]"> 
     <xsl:value-of select="@ID"/> 
    </xsl:for-each> 
</xsl:template> 
+0

该线的小修正。 'match =“type”'here ** type **,** T **应该是大写,然后它可以正常工作。 />' –

+0

+1代码不错.. –

+0

@Siva Charan:更正。谢谢。 –

1

在XSLT 2.0,你可以做到这一点的方式..

distinct-values(/SizeRanges/SizeRange/Types/Type/@ID) 

这给出了独特的ID作为输出