0

ХМL:

<?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="http://germany.gr">Germany</option>
<option value="france">France</option>
</select> 

也就是如果国家遇到几次,在value=中指定id。如果一次,则指定链接

说明分组如下:如果一个国家多次开会

代码:

<div id="france">
<p> <a href="/index.php?id=id1">Lion</a></p>
<p><a href="/index.php?id=id2">Paris</a></p>
</div>

如果该国遇到一次-没有写

4

2 回答 2

0

使用 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值的链接。

于 2012-08-12T14:10:53.273 回答
0

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kObjByCountry" match="Object" use="Property[@Name='COUNTRY']"/>

 <xsl:template match="/">
  <xsl:variable name="vCountries" select=
   "/*/*/*/data/Object
          [generate-id()
          = generate-id(key('kObjByCountry', Property[@Name='COUNTRY'])[1])
          ]"/>
  <select>
     <xsl:apply-templates select="$vCountries">
      <xsl:sort select="Property[@Name='COUNTRY']"/>
     </xsl:apply-templates>
    </select>

     <xsl:apply-templates select="$vCountries" mode="desc">
      <xsl:sort select="Property[@Name='COUNTRY']"/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="Object">
   <xsl:variable name="v2nd"
        select="key('kObjByCountry', Property[@Name='COUNTRY'])[2]"/>
   <option value="{(@Id[$v2nd]|Property[@Name='WWW'])[1]}">
    <xsl:value-of select="Property[@Name='COUNTRY']"/>
   </option>
 </xsl:template>

 <xsl:template mode="desc" 
    match="Object[key('kObjByCountry', Property[@Name='COUNTRY'])[2]]">
   <div id="sity_{@Id}">
     <xsl:apply-templates select=
      "key('kObjByCountry', Property[@Name='COUNTRY'])
                                    /Property[@Name='NAME']">
        <xsl:sort/>
      </xsl:apply-templates>
   </div>
 </xsl:template>

 <xsl:template match="Property[@Name='NAME']">
  <p><xsl:value-of select="."/></p>
 </xsl:template>

  <xsl:template mode="desc"
    match="Object[not(key('kObjByCountry', Property[@Name='COUNTRY'])[2])]"/>
</xsl:stylesheet>

应用于提供的 XML 文档时

<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="sity_id1">
   <p>Lion</p>
   <p>Paris</p>
</div>

说明

正确使用Muenchian 分组方法AVT s。

正确使用xsl:sort.

于 2012-08-12T14:14:51.360 回答