xslt元素上的组宽度(xslt 1.0)

时间:2012-08-12 13:18:56

标签: xslt xslt-1.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> 

也就是说,如果国家/地区满足几次,则在值=中指定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>

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

2 个答案:

答案 0 :(得分: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值的链接。< / p>

答案 1 :(得分: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 grouping method AVT

正确使用 xsl:sort