在嵌套 For Each 中的 XSLT 中排序 [英] Sorting in XSLT within nested For Each

查看:19
本文介绍了在嵌套 For Each 中的 XSLT 中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要做的:我有一个学院列表,每个学院内都有一个部门列表.我想显示整个部门列表,按部门名称排序,但要指明教职员工.

XML 如下所示:

<学院名称="科学"><部门名称="dept2"><head>X先生</head><building>A座</building>等等...</部门><部门名称="dept3"><head>X先生</head><building>B座</building>等等...</部门></教师><学院名称="教育"><部门名称="dept1"><head>Y先生</head><building>C座</building>等等...</部门></教师></院系>

XSLT 看起来像这样:(为了便于解释,我简化了 XSLT.)

<xsl:sort select="部门名称"><xsl:for-each select="Departments"><xsl:element name="div"><xsl:attribute name="id"><xsl:value-of select="facultName"></xsl:attribute><h3><xsl:value-of select="deptName">- <xsl:value-of select="facultName"></h3>//这里有更多的东西</xsl:element></xsl:for-each></xsl:for-each>

我希望输出如下所示:

Dept1(教育)负责人:Y先生建筑:C座Dept2(科学)负责人:X先生建筑:一个街区Dept3(科学)负责人:X先生建筑:B座

按部门名称排序的位置.

我还希望能够使用 Javascript 对特定教员隐藏所有系,即隐藏 ID 中具有特定教员的所有 div.

我什至不确定我的尝试是否可行(或合乎逻辑).我唯一的其他选择似乎是生成一个全新的部门列表,将教职员工作为要素之一.那么我只需要一个 for-each.不幸的是,我无法真正控制 XML 的生成方式,因此我希望能够以这种方式进行.

感谢您的帮助.谢谢!

解决方案

如果要按名称顺序列出所有系,不考虑系,直接遍历系即可

<xsl:sort select="@deptName"/></xsl:for-each>

然后,要获得系的教员名称,您可以很容易地访问父元素

所以,假设您有以下 XML

<Faculty id="1"facultyName="喝啤酒"><部门 id="1" deptName="Real Ale"/><部门 id="2" deptName="Lager"/></教师><Faculty id="2"facultyName="食物"><部门 id="3" deptName="鱼和薯条"/><部门 id="4" deptName="馅饼"/></教师></院系>

当您应用以下 XSLT 时

<xsl:output method="html" indent="yes"/><xsl:template match="/Faculties"><xsl:apply-templates select="教师/部门"><xsl:sort select="@deptName"/></xsl:apply-templates></xsl:模板><xsl:template match="部门"><div id="{../@facultyName}"><h3><xsl:value-of select="concat(@deptName, ' - ', ../@facultyName)"/></h3>

</xsl:模板></xsl:stylesheet>

以下是输出

<h3>鱼和薯条 - 食品</h3>

<div id="喝啤酒"><h3>拉格啤酒——喝啤酒</h3>

<div id="食物"><h3>馅饼 - 食物</h3>

<div id="喝啤酒"><h3>Real Ale - 喝啤酒</h3>

请注意,通常最好使用 xsl:apply-templates 而不是 xsl:for-each,所以这就是我在 XSLT 中使用的.

Here's what I'm trying to do: I have a list of Faculties, within each of which is a list of departments. I want to display the entire list of departments, sorted by Department name, but indicating the faculty.

The XML looks like this:

<Faculties>

  <Faculty Name="Science">
    <Department name="dept2">
      <head>Mr X</head>
      <building>A Block</building>
      etc...
   </Department>
   <Department name="dept3">
      <head>Mr X</head>
      <building>B Block</building>
      etc...
   </Department>
  </Faculty>

  <Faculty Name="Education">
    <Department name="dept1">
      <head>Mr Y</head>
      <building>C Block</building>
      etc...
    </Department>
  </Faculty>

</Faculties>    

The XSLT looks something like this: (I've simplified the XSLT for explanation purposes.)

<xsl:for-each select="Faculties">
  <xsl:sort select="DepartmentName">
  <xsl:for-each select="Departments">
    <xsl:element name="div">
      <xsl:attribute name="id"><xsl:value-of select="facultName"></xsl:attribute>
      <h3><xsl:value-of select="deptName"> - <xsl:value-of select="facultName"></h3>
      //More stuff here
    </xsl:element>
  </xsl:for-each>
</xsl:for-each>

I'd like the output to look like:

Dept1 (Education)
Head: Mr Y
Building: C Block

Dept2 (Science)
Head: Mr X
Building: A Block

Dept3 (Science)
Head: Mr X
Building: B Block

Where it's sorted by Department Name.

I also want to be able to hide all the departments from a particular faculty using Javascript, i.e. hide all divs which have a particular faculty in the id.

I'm not even sure if what I'm attempting is possible (or logical). My only other option seems to be generating a completely new list of departments, with faculty as one of the elements. Then I'll only need one for-each. Unfortunately, I can't really control how the XML is generated, so I'm hoping to be able to do it this way.

I appreciate any help. Thanks!

解决方案

If you want to list all departments in name order, regardless of the faculty, you can simply iterate over the departments directly

<xsl:for-each select="Faculty/Department">
   <xsl:sort select="@deptName" />

</xsl:for-each>

Then, to get the faculty name for the department, you can access the parent element quite easily

<xsl:value-of select="../@facultyName" />

So, assuming you have the following XML

<Faculties>
   <Faculty id="1" facultyName="Beer Drinking">
      <Department id="1" deptName="Real Ale" />
      <Department id="2" deptName="Lager" />
   </Faculty>
   <Faculty id="2" facultyName="Food">
      <Department id="3" deptName="Fish and Chips" />
      <Department id="4" deptName="Pies" />
   </Faculty>
</Faculties>

When you apply the following XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes"/>

   <xsl:template match="/Faculties">
      <xsl:apply-templates select="Faculty/Department">
         <xsl:sort select="@deptName" />
      </xsl:apply-templates>
   </xsl:template>

   <xsl:template match="Department">
      <div id="{../@facultyName}">
      <h3><xsl:value-of select="concat(@deptName, ' - ', ../@facultyName)" /></h3>
      </div>
   </xsl:template>
</xsl:stylesheet>

The following is output

<div id="Food">
   <h3>Fish and Chips - Food</h3>
</div>
<div id="Beer Drinking">
   <h3>Lager - Beer Drinking</h3>
</div>
<div id="Food">
   <h3>Pies - Food</h3>
</div>
<div id="Beer Drinking">
   <h3>Real Ale - Beer Drinking</h3>
</div>

Do note, it is usually preferably to use xsl:apply-templates over xsl:for-each, and so this is what I have used in the XSLT.

这篇关于在嵌套 For Each 中的 XSLT 中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆