-1

我希望你能在这个问题上为我指明正确的方向。我正在尝试获取组织结构图的过滤结果以显示每个部门。下面是源 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 数组进行过滤的帖子,比如使用 XSLT 数组的 XSLT 过滤器结果,但我的 xml 结构非常不同,以至于我没有运气让这种方法发挥作用。

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

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

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

4

1 回答 1

1

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

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

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

  <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">
于 2014-03-17T22:31:32.187 回答