使用 XSLT 1.0,您可以使用 Muechian 分组分别进行分组,以识别和消除重复;样式表
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:key name="k1" match="data/Object" use="@Id"/>
<xsl:template match="data">
<select>
<xsl:apply-templates select="Object[generate-id() = generate-id(key('k1', @Id)[1])]"/>
</select>
<xsl:apply-templates select="Object[generate-id() = generate-id(key('k1', @Id)[1]) and key('k1', @Id)[2]]" mode="desc"/>
</xsl:template>
<xsl:template match="data/Object[key('k1', @Id)[2]]">
<option value="{@Id}">
<xsl:value-of select="Property[@Name = 'COUNTRY']"/>
</option>
</xsl:template>
<xsl:template match="data/Object[not(key('k1', @Id)[2])]">
<option value="{Property[@Name = 'WWW']}">
<xsl:value-of select="Property[@Name = 'COUNTRY']"/>
</option>
</xsl:template>
<xsl:template match="data/Object" mode="desc">
<div id="city_{@Id}">
<xsl:apply-templates select="key('k1', @Id)/Property[@Name = 'NAME']" mode="desc"/>
</div>
</xsl:template>
<xsl:template match="data/Object/Property" mode="desc">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
转换输入
<page>
<elements>
<element name="bids">
<data>
<Object Id="id1">
<Property Name="NAME" Order="0">Paris</Property>
<Property Name="COUNTRY">France</Property>
<Property Name="WWW" >http://france.fr</Property>
</Object>
<Object Id="id1">
<Property Name="NAME" Order="0">Lion</Property>
<Property Name="COUNTRY">France</Property>
<Property Name="WWW" >http://france.fr</Property>
</Object>
<Object Id="id2">
<Property Name="NAME" Order="0">Berlin</Property>
<Property Name="COUNTRY">Germany</Property>
<Property Name="WWW" >http://germany.gr</Property>
</Object>
</data>
</element>
</elements>
</page>
进入
<select>
<option value="id1">France</option>
<option value="http://germany.gr">Germany</option></select><div id="city_id1">
<p>Paris</p>
<p>Lion</p>
</div>
[编辑]
这是一个修改后的样式表,它使用不同的键来尝试实现更改的需求:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="html" indent="yes"/>
<xsl:key name="k1" match="data/Object" use="Property[@Name = 'COUNTRY']"/>
<xsl:template match="data[Object]">
<select>
<xsl:apply-templates select="Object[generate-id() = generate-id(key('k1', Property[@Name = 'COUNTRY'])[1])]"/>
</select>
<xsl:apply-templates select="Object[generate-id() = generate-id(key('k1', Property[@Name = 'COUNTRY'])[1]) and key('k1', Property[@Name = 'COUNTRY'])[2]]" mode="desc"/>
</xsl:template>
<xsl:template match="data/Object[key('k1', Property[@Name = 'COUNTRY'])[2]]">
<option value="{Property[@Name = 'COUNTRY']}">
<xsl:value-of select="Property[@Name = 'COUNTRY']"/>
</option>
</xsl:template>
<xsl:template match="data/Object[not(key('k1', Property[@Name = 'COUNTRY'])[2])]">
<option value="{Property[@Name = 'WWW']}">
<xsl:value-of select="Property[@Name = 'COUNTRY']"/>
</option>
</xsl:template>
<xsl:template match="data/Object" mode="desc">
<div id="{Property[@Name = 'COUNTRY']}">
<xsl:apply-templates select="key('k1', Property[@Name = 'COUNTRY'])" mode="link">
<xsl:sort select="@Name"/>
</xsl:apply-templates>
</div>
</xsl:template>
<xsl:template match="data/Object" mode="link">
<p>
<a href="/index.php?id={@Id}">
<xsl:value-of select="@Name"/>
</a>
</p>
</xsl:template>
</xsl:stylesheet>
当我将该样式表应用于输入时
<?xml version="1.0" encoding="utf-8" ?>
<page>
<elements>
<element>
<data>
<Styles Name="default">
<Style Url="/css.css" Browser="default" Version="default"/>
</Styles>
</data>
</element>
<element type="Digillect.WB.Web.Elements.Site.SiteStructureElement">
<config StartLevel="0" MaxDepth="3" UseItemVisibility="false">
<monikers>
<moniker store="asdasd"/>
</monikers>
</config>
<data ParentPath="/">
</data>
</element>
<element name="bids">
<config>
<Object Id="1b61995a-6e22-4b09-af5f-9a50cdaa7863"/>
<Object Id="baa1d3df-0510-4f68-8a41-1b9b22587134"/>
</config>
<data>
<Object Id="id2" Name="Paris">
<Property Name="COUNTRY">France</Property>
<Property Name="WWW" >http://france.fr</Property>
</Object>
<Object Id="id1" Name="Lion">
<Property Name="COUNTRY">France</Property>
<Property Name="WWW" >http://france.fr</Property>
</Object>
<Object Id="id3" Name="Berlin">
<Property Name="COUNTRY">Germany</Property>
<Property Name="WWW" >http://germany.gr</Property>
</Object>
</data>
</element>
</elements>
</page>
结果是
<select>
<option value="France">France</option>
<option value="http://germany.gr">Germany</option></select><div id="France">
<p><a href="/index.php?id=id1">Lion</a></p>
<p><a href="/index.php?id=id2">Paris</a></p>
</div>
所以option
元素根据需要进行分组(尽管我无法弄清楚是什么决定了排序顺序)并且p
元素包含包含@Id
值的链接。