【问题标题】:Filter Org Chart Results - varying depth of hierarchy过滤组织结构图结果 - 层次结构的不同深度
【发布时间】:2014-03-17 18:50:53
【问题描述】:

我希望您能在这个问题上为我指明正确的方向。我正在尝试获取组织结构图的过滤结果以显示每个部门。下面是源 xml 的示例。如果有更好的方法可以根据我的要求进行重组,我可以重组 xml 源。

源 XML:

<OrgTree>
    <employee ID="1">
        <Name>John</Name>
        <Department>President's Office</Department>
        <employee ID="2">
            <Name>Ron</Name>
            <Department>President's Office</Department>
            <employee ID="3">
                <Name>Don</Name>
                <Department>CEO</Department>
            </employee>
        </employee>
        <employee ID="4">
            <Name>Mike</Name>
            <Department>Finance</Department>
            <employee ID="5">
                <Name>Mark</Name>
                <Department>Accounting</Department>
                <employee ID="6">
                    <Name>Marni</Name>
                    <Department>Accounting</Department>
                </employee>
            </employee>
            <employee ID="7">
                <Name>Mindy</Name>
                <Department>Investments</Department>
            </employee>
        </employee>
    </employee>
</OrgTree>  

我想获得两种不同形式的输出。

  1. 按部门过滤并获取该部门和子部门的所有节点(按财务树过滤)。

输出 1:

<OrgTree>
    <employee ID="4">
        <Name>Mike</Name>
        <Department>Finance</Department>
        <employee ID="5">
        <Name>Mark</Name>
            <Department>Accounting</Department>
            <employee ID="6">
                <Name>Marni</Name>
                <Department>Accounting</Department>
            </employee>
        </employee>
        <employee ID="7">
        <Name>Mindy</Name>
        <Department>Investments</Department>
        </employee>
    </employee>
</OrgTree>
  1. 按特定部门过滤输出并仅获取该部门中的节点

输出 2:

<OrgTree>
<employee ID="5">
    <Name>Mark</Name>
    <Department>Accounting</Department>
    <employee ID="6">
        <Name>Marni</Name>
        <Department>Accounting</Department>
    </employee>
</employee>
</OrgTree>

我发现了几篇关于像 XSLT Filter result using XSLT array 这样的过滤的帖子,但是我的 xml 结构非常不同,以至于我没有运气让这种方法发挥作用。

我在 SharePoint 中,所以我正在尝试按部门过滤的 xsl 在下面,但我没有得到任何结果,因为 xml 中只有一个父行。我已经使用了许多不同的方法在 rowview 模板中进行过滤,但我只想将过滤后的结果传递给 rowview 模板,而不必遍历那里的所有节点。考虑到子节点的不同深度,我不知道如何做到这一点。

<xsl:for-each select="$Rows[Department = 'CFO']">
     <xsl:call-template name="dvt_1.rowview"/>
</xsl:for-each>

任何帮助,指导将不胜感激。

【问题讨论】:

  • 您几乎没有包含任何代码。或许可以尝试多写一些。
  • 感谢版权噩梦的反馈。我本可以粘贴 for each 的 20 种变体,然后选择我尝试过的语句。但它们并没有按照我需要的方式工作。正如我所说,我不知道如何遍历未知深度的节点。而我在 dvt_1.rowview 中为每个员工绘制组织结构图的代码与手头的问题无关。
  • 如果代码那么敏感,也许这是一个你不能在公共论坛上问的问题。试着把问题分解成更小的部分,然后你可以从那里找出解决方案。

标签: xml xslt filter organizational-chart


【解决方案1】:

您可能会从使用身份模板开始,该模板可用于复制节点而无需更改

<xsl:template match="@*|node()" name="identity">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

然后,您将拥有一个用于 employee 元素的模板,您可以在其中决定是否要复制它们。如果他们有一个与您想要的部门相匹配的部门,或者有一个与一个祖先相匹配的部门,那么您将复制它。否则,您将继续处理子 employee 元素

  <xsl:choose>
     <xsl:when test="ancestor-or-self::employee[Department=$dept]">
        <xsl:call-template name="identity"/>
     </xsl:when>
     <xsl:otherwise>
        <xsl:apply-templates select="employee"/>
     </xsl:otherwise>
  </xsl:choose>

(其中 $dept 是一个包含您要过滤的部门名称的参数)

试试这个 XSLT

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

   <xsl:param name="dept" select="'Finance'"/>

   <xsl:template match="@*|node()" name="identity">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="employee">
      <xsl:choose>
         <xsl:when test="ancestor-or-self::employee[Department=$dept]">
            <xsl:call-template name="identity"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:apply-templates select="employee"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

这应该给你“输出 1”。对于“输出 2”,只需将条件更改为此

<xsl:when test="self::employee[Department=$dept]">

或者更好,就这个

<xsl:when test="Department=$dept">

【讨论】:

  • 谢谢蒂姆,我不熟悉身份模板方法。
  • 谢谢蒂姆,我对身份模板方法还不够熟悉。这确实给了我正在寻找的输出,但我意识到也许我分阶段提出了我不是在寻找 xml 输出的问题,而是一种通过将过滤器的输出发送到“行视图”模板。我可以这样做吗?
  • 嗯...我想你可能需要问一个全新的问题!
猜你喜欢
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
相关资源
最近更新 更多