使用属性的 XSL 分组 [英] XSL grouping using attribute
本文介绍了使用属性的 XSL 分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些看起来像这样的 XML:
I've got some XML that looks a bit like this:
<Options>
<Option OptionGroup="Safety">Driver Airbag</Option>
<Option OptionGroup="Comfort and Convenience">Front Electric Windows</Option>
<Option OptionGroup="Comfort and Convenience">Height Adjustable Drivers Seat</Option>
<Option OptionGroup="Comfort and Convenience">Pollen Filter</Option>
<Option OptionGroup="Comfort and Convenience">Privacy Glass</Option>
<Option OptionGroup="Comfort and Convenience">Remote Boot Release</Option>
<Option OptionGroup="Comfort and Convenience">Split Rear Seats</Option>
<Option>Something else</Option>
<Option>Yet another thing</Option>
<Option OptionGroup="Paint or Trim and Upholstery">Metallic Paint</Option>
<Option OptionGroup="Exterior Equipment">Power Assisted Steering (PAS)</Option>
<Option OptionGroup="Interior Equipment">Leather Steering Wheel</Option>
<Option OptionGroup="In Car Entertainment">CD Radio</Option>
<Option OptionGroup="Security">Central Locking</Option>
</Options>
我想使用 XSL 1.0 将输出分组在一起,基于他们的 OptionGroup 属性,在末尾列出任何没有列出属性的选项.类似的东西:
I'd like to group the output together using XSL 1.0, based on their OptionGroup attirbute, having any Options without an attribute listed at the end. Something like:
<table>
<tr><th>Safety</th></tr>
<tr><td>Drivers Airbag</td></tr>
<tr><th>Comfort and Convenience</th></tr>
<tr><td>Front Electric Windows, Height Adjustable Drivers Seat, Pollen Filter, Privacy Glass, Remote Boot Release, Split Rear Seats</td></tr>
<tr><th>Paint or Trim and Upholstery</th></tr>
<tr><td>Metallic Paint</td></tr>
<tr><th>Exterior Equipment</th></tr>
<tr><td>Power Assisted Steering (PAS)</td></tr>
<tr><th>Interior Equipment</th></tr>
<tr><td>CD Radio</td></tr>
<tr><th>Security</th></tr>
<tr><td>Central Locking</td></tr>
<tr><th>Other</th></tr>
<tr><td>Something else, Yet another thing</td></tr>
</table>
我猜这都是可能的.
推荐答案
这应该有效:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<table>
<xsl:for-each select="Options/Option/@OptionGroup[not(. = preceding::Option/@OptionGroup)]">
<xsl:call-template name="OutputGroup">
<xsl:with-param name="title" select="." />
<xsl:with-param name="items" select="../../Option[@OptionGroup = current()]" />
</xsl:call-template>
</xsl:for-each>
<xsl:if test="Options/Option[not(@OptionGroup)]">
<xsl:call-template name="OutputGroup">
<xsl:with-param name="title" select="'Other'" />
<xsl:with-param name="items" select="Options/Option[not(@OptionGroup)]" />
</xsl:call-template>
</xsl:if>
</table>
</xsl:template>
<xsl:template name="OutputGroup">
<xsl:param name="title" />
<xsl:param name="items" />
<tr>
<th>
<xsl:value-of select="$title"/>
</th>
</tr>
<tr>
<td>
<xsl:apply-templates select="$items" />
</td>
</tr>
</xsl:template>
<xsl:template match="Option">
<xsl:if test="position() > 1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
这篇关于使用属性的 XSL 分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文