XML使用XSLT按组生成列

时间:2011-09-23 21:32:56

标签: html xml xslt

我正在尝试生成一个4列目录,按我所在部门的4个学科分组。一个XML文件包含整个组。每个元素都有一个部门标记。目录的构造如下:

按纪律对每个条目进行分组 对于每个组,循环每个条目。如果等级等于主管,请填写主管DIV,否则为该组中的每个人生成一个div。 一旦组中的所有条目都用完,就构建下一组的下一列...... 继续前进,直到所有人都筋疲力尽。

我是XSLT的新手,真的需要帮助。我可以为每个组创建一个密钥,并循环访问组中的条目,但我不确定如何循环遍历不同的组。

我的标记在下面。

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="group" match="employee" use="dept" />


<xsl:template match="/">

<div id="directory">    
    <div class="man">
        <h1>Manager</h1>
        <h2>John Doe</h2>
    </div>


    <div class="group">
<xsl:for-each select="key('group', 'Mechanical')">
<xsl:sort select="name" order="ascending"/>

                (I need a conditional here to check if rank = supervisor)
        <div class="super">
            <h1>Supervisor</h1>
            <h2><xsl:value-of select="name"/></h2>
        </div>

            <div>
                <p class="name"><xsl:value-of select="name"/></p>
                                    <p class="ID"><xsl:value-of select="id"/></p>

            </div>
  </xsl:for-each>
</div>

XML目录

<?xml version="1.0" encoding="ISO-8859-1" ?> 

<directory>
  <employee>
    <dept></dept>
    <rank>Manager</rank>
    <name>John Doe</name>
    <id>1234</id>
  </employee>
  <employee>
    <dept>Mechanical</dept>
    <rank>Supervisor</rank>
    <name>Jane Doe</name>
    <id>4321</id>
  </employee>
  <employee>
    <dept>Mechanical</dept>
    <rank>General</rank>
    <name>Joe Doe</name>
    <id>2314</id>
  </employee>
  <employee>
    <dept>Mechanical</dept>
    <rank>General</rank>
    <name>Joe Doe</name>
    <id>2314</id>
  </employee> 
  <employee>
    <dept>Civil</dept>
    <rank>Supervisor</rank>
    <name>Jane Doe</name>
    <id>4321</id>
  </employee>
  <employee>
    <dept>Civil</dept>
    <rank>General</rank>
    <name>Joe Doe</name>
    <id>2314</id>
  </employee>
  <employee>
    <dept>Civil</dept>
    <rank>General</rank>
    <name>Joe Doe</name>
    <id>2314</id>
  </employee>
  <employee>
    <dept>Electrical</dept>
    <rank>Supervisor</rank>
    <name>Jane Doe</name>
    <id>4321</id>
  </employee>
  <employee>
    <dept>Electrical</dept>
    <rank>General</rank>
    <name>Joe Doe</name>
    <id>2314</id>
  </employee>
  <employee>
    <dept>Electrical</dept>
    <rank>General</rank>
    <name>Joe Doe</name>
    <id>2314</id>
  </employee>
 </directory>

输出看起来像: directory

HTML应该类似于:

<div id="directory">    
<div class="man">
<h1>Manager</h1>
<h2>John Doe</h2>
</div>
<div class="group">
    <div class="super">
        <h1>Mechanical Supervisor</h1>
        <h2>Supervisor Name</h2>
    </div>
    <div>
        <p class="name">Mech employee name</p>
                    <p class="ID">Mech employee ID</p>
</div><!--end group A-->

<div class="group">
    <div class="super">
        <h1>CivilSupervisor</h1>
        <h2>Supervisor Name</h2>
    </div>
    <div>
        <p class="name">Civil employee name</p>
                    <p class="ID">Civil employee ID</p>
</div><!--end group B-->

<div class="group">
    <div class="super">
        <h1>Electrical Supervisor</h1>
        <h2>Supervisor Name</h2>
    </div>
    <div>
        <p class="name">Electrical employee name</p>
                    <p class="ID">Electrical employee ID</p>
</div><!--end group C-->
</div>

1 个答案:

答案 0 :(得分:2)

此XSLT 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:strip-space elements="*"/>

 <xsl:key name="kEmpByDeptRank" match="employee"
          use="concat(dept,'+', rank)"/>

 <xsl:key name="kEmpByDept" match="employee"
          use="dept"/>

 <xsl:template match="directory">
   <div id="directory">
     <xsl:apply-templates select=
      "key('kEmpByDeptRank', '+Manager')"/>
      <xsl:apply-templates select=
        "employee[not(rank='Manager')]"/>
   </div>
 </xsl:template>

 <xsl:template match=
  "employee[generate-id()
           =
            generate-id(key('kEmpByDept', dept)[1])
           ]
  ">
  <div class="group">
    <xsl:apply-templates mode="inGroup" select=
    "key('kEmpByDeptRank', concat(dept,'+Supervisor'))"/>

    <xsl:apply-templates mode="inGroup" select=
      "key('kEmpByDept', dept)[not(rank='Supervisor')]"/>
  </div>
 </xsl:template>

 <xsl:template match="employee[rank='Manager']">
   <div class="man">
     <h1>Manager</h1>
     <h2><xsl:value-of select="name"/></h2>
   </div>
 </xsl:template>

 <xsl:template match="employee[rank='Supervisor']"
      mode="inGroup">
    <div class="super">
      <h1>
        <xsl:value-of select="concat(dept, ' Supervisor')"/>
      </h1>
      <h2><xsl:value-of select="name"/></h2>
    </div>
 </xsl:template>

 <xsl:template match="employee" mode="inGroup">
   <div>
     <p class="name">
       <xsl:value-of select="concat(dept, ' ', name)"/>
     </p>
     <p class="ID">
       <xsl:value-of select="concat(dept, ' ', id)"/>
     </p>
   </div>
 </xsl:template>
 <xsl:template match="employee"/>
</xsl:stylesheet>

应用于此XML文档(类似于提供的文档,但将名称和ID更改为不同):

<directory>
  <employee>
    <dept></dept>
    <rank>Manager</rank>
    <name>John Doe</name>
    <id>1234</id>
  </employee>
  <employee>
    <dept>Mechanical</dept>
    <rank>Supervisor</rank>
    <name>Jane Doe</name>
    <id>4321</id>
  </employee>
  <employee>
    <dept>Mechanical</dept>
    <rank>General</rank>
    <name>Joe Doe</name>
    <id>2314</id>
  </employee>
  <employee>
    <dept>Mechanical</dept>
    <rank>General</rank>
    <name>Jim Smith</name>
    <id>2315</id>
  </employee>
  <employee>
    <dept>Civil</dept>
    <rank>Supervisor</rank>
    <name>Ann Smith</name>
    <id>4322</id>
  </employee>
  <employee>
    <dept>Civil</dept>
    <rank>General</rank>
    <name>Peter Pan</name>
    <id>2316</id>
  </employee>
  <employee>
    <dept>Civil</dept>
    <rank>General</rank>
    <name>Mike Sims</name>
    <id>2317</id>
  </employee>
  <employee>
    <dept>Electrical</dept>
    <rank>Supervisor</rank>
    <name>Amy Dull</name>
    <id>4323</id>
  </employee>
  <employee>
    <dept>Electrical</dept>
    <rank>General</rank>
    <name>Dan Brown</name>
    <id>2318</id>
  </employee>
  <employee>
    <dept>Electrical</dept>
    <rank>General</rank>
    <name>John Kerry</name>
    <id>2319</id>
  </employee>
 </directory>

生成想要的正确结果

<div id="directory">
   <div class="man">
      <h1>Manager</h1>
      <h2>John Doe</h2>
   </div>
   <div class="group">
      <div class="super">
         <h1>Mechanical Supervisor</h1>
         <h2>Jane Doe</h2>
      </div>
      <div>
         <p class="name">Mechanical Joe Doe</p>
         <p class="ID">Mechanical 2314</p>
      </div>
      <div>
         <p class="name">Mechanical Jim Smith</p>
         <p class="ID">Mechanical 2315</p>
      </div>
   </div>
   <div class="group">
      <div class="super">
         <h1>Civil Supervisor</h1>
         <h2>Ann Smith</h2>
      </div>
      <div>
         <p class="name">Civil Peter Pan</p>
         <p class="ID">Civil 2316</p>
      </div>
      <div>
         <p class="name">Civil Mike Sims</p>
         <p class="ID">Civil 2317</p>
      </div>
   </div>
   <div class="group">
      <div class="super">
         <h1>Electrical Supervisor</h1>
         <h2>Amy Dull</h2>
      </div>
      <div>
         <p class="name">Electrical Dan Brown</p>
         <p class="ID">Electrical 2318</p>
      </div>
      <div>
         <p class="name">Electrical John Kerry</p>
         <p class="ID">Electrical 2319</p>
      </div>
   </div>
</div>

,它在浏览器中显示为

          

管理器

      

John Doe

                        

机械主管

         

Jane Doe

                       机械Joe Doe

         机械2314

                       机械吉姆史密斯

         机械2315

                               

民事监督员

         <2> Ann Smith                        民间小飞侠

         Civil 2316

                       民间迈克西姆斯

         Civil 2317

                               

电气监督员

         

Amy Dull

                       电气丹布朗

         电气2318

                       电气约翰克里

         电气2319

          

解释 Muenchian method for grouping ,并使用复合密钥employee找到(rank, department)(经理或主管)。