从XML构建XSL树

从XML构建XSL树,xml,xslt,dhtmlx,Xml,Xslt,Dhtmlx,从几天/一周开始,我就开始头疼,所以现在我想是时候问一些新的建议了 *这是我在XML/XSL中的第一个项目,我将尽可能清晰 我从一个SQL查询中获得了一个XML输出,该输出如下所示: <row DEPARTMENT="SOCIETY" DEPARTMENT_LEVEL="0" CHILD_DEPARTMENT="1111" PARENT_DEPARTMENT="1111"/> <row DEPARTMENT="Boss" DEPARTMENT_LEVEL="1" CHILD

从几天/一周开始,我就开始头疼,所以现在我想是时候问一些新的建议了

*这是我在XML/XSL中的第一个项目,我将尽可能清晰

我从一个SQL查询中获得了一个XML输出,该输出如下所示:

<row DEPARTMENT="SOCIETY" DEPARTMENT_LEVEL="0" CHILD_DEPARTMENT="1111" PARENT_DEPARTMENT="1111"/> 

<row DEPARTMENT="Boss" DEPARTMENT_LEVEL="1" CHILD_DEPARTMENT="2222" PARENT_DEPARTMENT="1111"/>

<row DEPARTMENT="Second Boss" DEPARTMENT_LEVEL="1" CHILD_DEPARTMENT="3333" PARENT_DEPARTMENT="1111"/>

<row DEPARTMENT="Secretary" DEPARTMENT_LEVEL="1" CHILD_DEPARTMENT="4444" PARENT_DEPARTMENT="1111"/>

<row DEPARTMENT="Desk" DEPARTMENT_LEVEL="2" CHILD_DEPARTMENT="5555" PARENT_DEPARTMENT="4444"/>

<row DEPARTMENT="Chief" DEPARTMENT_LEVEL="3" CHILD_DEPARTMENT="6666" PARENT_DEPARTMENT="5555"/>

<row DEPARTMENT="post1" DEPARTMENT_LEVEL="4" CHILD_DEPARTMENT="7777" PARENT_DEPARTMENT="6666"/>

<row DEPARTMENT="post2" DEPARTMENT_LEVEL="4" CHILD_DEPARTMENT="8888" PARENT_DEPARTMENT="6666"/>

<row DEPARTMENT="post3" DEPARTMENT_LEVEL="4" CHILD_DEPARTMENT="9999" PARENT_DEPARTMENT="6666"/>
<tree id="0">
<item id="1111" text="Society">
  <item id="2222" text="Boss"/>
  <item id="3333" text="Second Boss"/>
  <item id="4444" text="Secretary">
     <item id="5555" text="Desk">
        <item id="6666" text="Chief">
           <item id="7777" text="post1"/>
           <item id="8888" text="post2"/>
           <item id="9999" text="post3"/>
        </item>
     </item>
  </item>
</item>
</tree>

然后我正在搜索用XSL构建一个树:

<xsl:template match='/'>
<html>
<body>
    <ul>
    <tree id='0'>
        <li><item>
        <xsl:attribute name ='id'>1111</xsl:attribute>
        <xsl:attribute name ='text'>Society</xsl:attribute>
        <xsl:apply-templates select='//row' />
        </item></li>
    </tree>
    </ul>
</body>
</html>
</xsl:template>
<xsl:template match='//row'>
<li><item>
    <xsl:attribute name ='id'>
    <xsl:value-of select='@CHILD_DEPARTMENT' />
    </xsl:attribute>
        <xsl:attribute name ='text'>
    <xsl:value-of select='@DEPARTMENT' />
    </xsl:attribute>
</item></li>
</xsl:template>

  • 1111 社会
  • 我需要获得格式良好的数据,以便将它们放入DHTMLX树中,它必须如下所示:

    <row DEPARTMENT="SOCIETY" DEPARTMENT_LEVEL="0" CHILD_DEPARTMENT="1111" PARENT_DEPARTMENT="1111"/> 
    
    <row DEPARTMENT="Boss" DEPARTMENT_LEVEL="1" CHILD_DEPARTMENT="2222" PARENT_DEPARTMENT="1111"/>
    
    <row DEPARTMENT="Second Boss" DEPARTMENT_LEVEL="1" CHILD_DEPARTMENT="3333" PARENT_DEPARTMENT="1111"/>
    
    <row DEPARTMENT="Secretary" DEPARTMENT_LEVEL="1" CHILD_DEPARTMENT="4444" PARENT_DEPARTMENT="1111"/>
    
    <row DEPARTMENT="Desk" DEPARTMENT_LEVEL="2" CHILD_DEPARTMENT="5555" PARENT_DEPARTMENT="4444"/>
    
    <row DEPARTMENT="Chief" DEPARTMENT_LEVEL="3" CHILD_DEPARTMENT="6666" PARENT_DEPARTMENT="5555"/>
    
    <row DEPARTMENT="post1" DEPARTMENT_LEVEL="4" CHILD_DEPARTMENT="7777" PARENT_DEPARTMENT="6666"/>
    
    <row DEPARTMENT="post2" DEPARTMENT_LEVEL="4" CHILD_DEPARTMENT="8888" PARENT_DEPARTMENT="6666"/>
    
    <row DEPARTMENT="post3" DEPARTMENT_LEVEL="4" CHILD_DEPARTMENT="9999" PARENT_DEPARTMENT="6666"/>
    
    <tree id="0">
    <item id="1111" text="Society">
      <item id="2222" text="Boss"/>
      <item id="3333" text="Second Boss"/>
      <item id="4444" text="Secretary">
         <item id="5555" text="Desk">
            <item id="6666" text="Chief">
               <item id="7777" text="post1"/>
               <item id="8888" text="post2"/>
               <item id="9999" text="post3"/>
            </item>
         </item>
      </item>
    </item>
    </tree>
    
    
    
    下面是我的代码:

    <tree id="0">
    <item id="1111" text="Society"/>
        <item id="2222" text="Boss"/>
        <item id="3333" text="Second Boss"/>
        <item id="4444" text="Secretary"/>
            <item id="5555" text="Desk"/>
            <item id="6666" text="Chief"/>
            <item id="7777" text="post1"/>
            <item id="8888" text="post2"/>
            <item id="9999" text="post3"/>
    </item>
    </tree>
    
    
    
    我选择对“树”和第一个“项”元素进行harcode编码,以获得结构良好的开头

    正如您可能注意到的,此代码中的任何内容都无法测试它是否必须是新元素或是否是子元素

    我尝试了很多东西,比如:

  • 子/父属性上的条件测试=>不起作用,因为不在每个“行”上应用测试
  • 使用variable存储父属性=>的值不起作用,因为声明后无法更改变量的值
  • 因此,我真的不知道如何找到构建此树的解决方案:(

    如果有人给我一些建议,那将非常有用

    Ps:对不起,有些拼写错误,英语不是我的第一语言


    关于模板匹配中的,
    /
    您应该从只选择部门级别为0的
    元素开始

    <xsl:apply-templates select="//row[@DEPARTMENT_LEVEL='0']" />
    
    试试这个XSLT

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method="xml" indent="yes" />
    
    <xsl:template match='/'>
        <tree id='0'>
            <xsl:apply-templates select="//row[@DEPARTMENT_LEVEL='0']" />
        </tree>
    </xsl:template>
    
    <xsl:template match='//row'>
        <item>
            <xsl:attribute name ='id'>
                <xsl:value-of select='@CHILD_DEPARTMENT' />
            </xsl:attribute>
            <xsl:attribute name ='text'>
                <xsl:value-of select='@DEPARTMENT' />
            </xsl:attribute>
            <xsl:apply-templates select="//row[@PARENT_DEPARTMENT = current()/@CHILD_DEPARTMENT and @DEPARTMENT_LEVEL = current()/@DEPARTMENT_LEVEL + 1]" />
        </item>
    </xsl:template>
    </xsl:stylesheet>
    

    还请注意使用“属性值模板”来设置
    元素的属性。

    在不测试自己的情况下,这对我来说似乎是明智的。第一个表:谢谢!它工作正常,我将分析第二个“应用模板”,以确保了解它真正的功能!你让我开心:-)