使用XML和XSLT进行树导航

使用XML和XSLT进行树导航,xml,xslt,xhtml,treeview,Xml,Xslt,Xhtml,Treeview,我有一个树结构的XML(toc.XML): 每个密钥在文档中都是唯一的 我有一个XSLT,它导入toc XML并尝试输出导航: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="no" /> <xsl:variable name="id" select="/member/@id"

我有一个树结构的XML(toc.XML):


每个密钥在文档中都是唯一的

我有一个XSLT,它导入toc XML并尝试输出导航:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="no" />
  <xsl:variable name="id" select="/member/@id" />

  <xsl:template match="/member">
    <html>
      <head>
        <title><xsl:value-of select="/member/name"/></title>
      </head>
      <body>
        <div class="navigation">
          <xsl:apply-templates select="document('toc.xml')" />
        </div>
        <div class="content">
          <xsl:apply-templates />
        </div>
      </body>
    </html>
  </xsl:template>
</xsl>

并希望找到一个节点并在HTML文件中输出以下HTML:

...html...
<div class="navigation">
  <ul>
    <li><a href="#">top</a><ul>
      <li><a href="#">child1</li><ul>
        <li><a href="#">child2</li><ul>
            <li><a href="#">child3-1</a></li>
            <li><a href="#">child3-2</a></li>
          </ul></li>
        </ul></li>
      </ul></li>
  </ul>
</div>
...more html...
…html。。。
…更多html。。。
基本上我想搜索一个节点:item[@key='4294967611'],并输出所有父节点和直接子节点


我觉得这应该很容易,但我正在努力寻找如何做到这一点的信息。我的XSLT知识不是很好。

这是正确的实现,它为ulli元素提供了适当的父子层次结构。它将只输出xsl:variable keyVar中给定键的父节点或直接子节点

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"    >
  <xsl:output method="xml" indent="yes"/>
  <xsl:variable name ="keyVar" select="'4294967666'" />

  <xsl:template match="toc">
    <ul>
      <xsl:apply-templates select="item" />
    </ul>
  </xsl:template>

  <xsl:template match="item">
    <li>
      <a href="#" />
      <xsl:value-of select="@name"/>
      <xsl:if test ="count(descendant::item) > 0">
        <ul>
          <xsl:apply-templates select="item[.//@key=$keyVar or ../@key = $keyVar]" />
        </ul>
      </xsl:if>
    </li>

  </xsl:template>
</xsl:stylesheet>

  • 输出层次结构:

    <?xml version="1.0" encoding="utf-8"?>
    <ul>
      <li>top<ul>
        <li>child1<ul>
          <li>child2-1<ul>
            <li>child3-1</li>
            <li>child3-2</li>
          </ul></li>
          <li>child2-2<ul>
            <li>d</li>
          </ul></li>
          <li>child2-3<ul>
            <li>d</li>
            <li>PI</li>
            <li>q</li>
          </ul></li>
          <li>child2-4</li>
          <li>child2-5<ul>
            <li>bb</li>
            <li>bb</li>
            <li>f</li>
          </ul></li>
        </ul></li>
      </ul></li>
    </ul>
    
    
    
    • 顶部
      • 儿童1
        • 儿童2-1
          • 儿童3-1
          • 儿童3-2
        • 儿童2-2
          • d
        • 儿童2-3
          • d
          • 圆周率
          • q
        • 儿童2-4
        • 儿童2-5
          • bb
          • bb
          • f

    首先,提供的想要的输出不是格式良好的XML文档或片段

    此外,在提供的源XML文档中没有
    元素

    另外,有六个
    元素带有
    key='4294967611'
    ,因此这也不是识别元素的可用标准

    我想,你想要这个

    <ul>
      <li><a href="#">top</a>
          <ul>
            <li><a href="#">child1</a>
            <ul>
              <li><a href="#">child2-1</a>
                  <ul>
                      <li><a href="#">child3-1</a></li>
                      <li><a href="#">child3-2</a></li>
                  </ul>
              </li>
            </ul>
           </li>
          </ul>
      </li>
    </ul>
    
    <toc>
    <item name="top" key="4294967296" subkey="1">
        <item name="child1" key="4294967611" subkey="">
            <item name="child2-1" key="4294967611" subkey="">
                <item name="child3-1" key="4294967613" subkey=""/>
                <item name="child3-2" key="4294967612" subkey=""/>
            </item>
            <item name="child2-2" key="4294967611" subkey="">
                <item name="d" key="4294974806" subkey=""/>
            </item>
            <item name="child2-3" key="4294967611" subkey="">
                <item name="d" key="4294967661" subkey=""/>
                <item name="PI" key="4294967659" subkey=""/>
                <item name="q" key="4294967660" subkey=""/>
            </item>
            <item name="child2-4" key="4294967611" subkey=""/>
            <item name="child2-5" key="4294967611" subkey="">
                <item name="bb" key="4294967616" subkey=""/>
                <item name="bb" key="4294967620" subkey=""/>
                <item name="f" key="4294967615" subkey=""/>
            </item>
        </item>
    </item>
    </toc>
    
    <ul>
       <li>
          <a href="#">top</a>
          <ul>
             <li>
                <a href="#">child1</a>
                <ul>
                   <li>
                      <a href="#">child2-1</a>
                      <ul>
                         <li>
                            <a href="#">child3-1</a>
                         </li>
                         <li>
                            <a href="#">child3-2</a>
                         </li>
                      </ul>
                   </li>
                </ul>
             </li>
          </ul>
       </li>
    </ul>
    
    如果我的猜测是正确的,这里有一个可能的解决方案:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:param name="pEl" select="//item[@name='child2-1']"/>
    
     <xsl:template match="toc">
       <xsl:variable name="pChain" select="$pEl/ancestor-or-self::item"/>
       <xsl:apply-templates select="$pChain[1]">
        <xsl:with-param name="pChain" select="$pChain"/>
        <xsl:with-param name="pEndElementId" select="generate-id($pEl)"/>
       </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="item">
        <xsl:param name="pChain" select="/.."/>
        <xsl:param name="pcurPosition" select="1"/>
        <xsl:param name="pEndElementId"/>
        <ul>
          <li><a href="#"><xsl:value-of select="@name"/></a>
            <xsl:choose>
             <xsl:when test="generate-id() = $pEndElementId">
              <ul>
               <xsl:apply-templates select="item" mode="leafChildren"/>
              </ul>
             </xsl:when>
             <xsl:otherwise>
               <xsl:apply-templates select="$pChain[position()=$pcurPosition+1]">
                <xsl:with-param name="pChain" select="$pChain"/>
                <xsl:with-param name="pcurPosition" select="$pcurPosition +1"/>
                <xsl:with-param name="pEndElementId" select="$pEndElementId"/>
               </xsl:apply-templates>
             </xsl:otherwise>
            </xsl:choose>
          </li>
        </ul>
     </xsl:template>
    
     <xsl:template match="item" mode="leafChildren">
      <li><a href="#"><xsl:value-of select="@name"/></a></li>
     </xsl:template>
    </xsl:stylesheet>
    
    
    
  • 当此转换应用于提供的XML文档时

    <ul>
      <li><a href="#">top</a>
          <ul>
            <li><a href="#">child1</a>
            <ul>
              <li><a href="#">child2-1</a>
                  <ul>
                      <li><a href="#">child3-1</a></li>
                      <li><a href="#">child3-2</a></li>
                  </ul>
              </li>
            </ul>
           </li>
          </ul>
      </li>
    </ul>
    
    <toc>
    <item name="top" key="4294967296" subkey="1">
        <item name="child1" key="4294967611" subkey="">
            <item name="child2-1" key="4294967611" subkey="">
                <item name="child3-1" key="4294967613" subkey=""/>
                <item name="child3-2" key="4294967612" subkey=""/>
            </item>
            <item name="child2-2" key="4294967611" subkey="">
                <item name="d" key="4294974806" subkey=""/>
            </item>
            <item name="child2-3" key="4294967611" subkey="">
                <item name="d" key="4294967661" subkey=""/>
                <item name="PI" key="4294967659" subkey=""/>
                <item name="q" key="4294967660" subkey=""/>
            </item>
            <item name="child2-4" key="4294967611" subkey=""/>
            <item name="child2-5" key="4294967611" subkey="">
                <item name="bb" key="4294967616" subkey=""/>
                <item name="bb" key="4294967620" subkey=""/>
                <item name="f" key="4294967615" subkey=""/>
            </item>
        </item>
    </item>
    </toc>
    
    <ul>
       <li>
          <a href="#">top</a>
          <ul>
             <li>
                <a href="#">child1</a>
                <ul>
                   <li>
                      <a href="#">child2-1</a>
                      <ul>
                         <li>
                            <a href="#">child3-1</a>
                         </li>
                         <li>
                            <a href="#">child3-2</a>
                         </li>
                      </ul>
                   </li>
                </ul>
             </li>
          </ul>
       </li>
    </ul>
    
    
    
    产生所需结果

    <ul>
      <li><a href="#">top</a>
          <ul>
            <li><a href="#">child1</a>
            <ul>
              <li><a href="#">child2-1</a>
                  <ul>
                      <li><a href="#">child3-1</a></li>
                      <li><a href="#">child3-2</a></li>
                  </ul>
              </li>
            </ul>
           </li>
          </ul>
      </li>
    </ul>
    
    <toc>
    <item name="top" key="4294967296" subkey="1">
        <item name="child1" key="4294967611" subkey="">
            <item name="child2-1" key="4294967611" subkey="">
                <item name="child3-1" key="4294967613" subkey=""/>
                <item name="child3-2" key="4294967612" subkey=""/>
            </item>
            <item name="child2-2" key="4294967611" subkey="">
                <item name="d" key="4294974806" subkey=""/>
            </item>
            <item name="child2-3" key="4294967611" subkey="">
                <item name="d" key="4294967661" subkey=""/>
                <item name="PI" key="4294967659" subkey=""/>
                <item name="q" key="4294967660" subkey=""/>
            </item>
            <item name="child2-4" key="4294967611" subkey=""/>
            <item name="child2-5" key="4294967611" subkey="">
                <item name="bb" key="4294967616" subkey=""/>
                <item name="bb" key="4294967620" subkey=""/>
                <item name="f" key="4294967615" subkey=""/>
            </item>
        </item>
    </item>
    </toc>
    
    <ul>
       <li>
          <a href="#">top</a>
          <ul>
             <li>
                <a href="#">child1</a>
                <ul>
                   <li>
                      <a href="#">child2-1</a>
                      <ul>
                         <li>
                            <a href="#">child3-1</a>
                         </li>
                         <li>
                            <a href="#">child3-2</a>
                         </li>
                      </ul>
                   </li>
                </ul>
             </li>
          </ul>
       </li>
    </ul>
    
    如果我对想要的结果的猜测没有成功,请更正您的问题并提供格式良好的输出。

    使用提供的输入(没有唯一的
    @key
    ),此样式表:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="key" select="4294967611"/>
        <xsl:template match="item">
            <xsl:choose>
                <xsl:when test="generate-id() =
                                generate-id(../item[@key=$key][1])
                                and
                                not(item[@key=$key])">
                    <xsl:call-template name="chain">
                        <xsl:with-param name="parents" select="ancestor-or-self::item"/>
                        <xsl:with-param name="childs" select="item"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
        <xsl:template name="chain">
            <xsl:param name="parents"/>
            <xsl:param name="childs"/>
            <xsl:if test="$parents">
                <ul>
                    <li>
                        <a href="#">
                            <xsl:value-of select="$parents[1]/@name"/>
                        </a>
                        <xsl:call-template name="chain">
                            <xsl:with-param name="parents" select="$parents[position()!=1]"/>
                            <xsl:with-param name="childs" select="$childs"/>
                        </xsl:call-template>
                        <xsl:if test="count($parents)=1">
                            <ul>
                                <xsl:for-each select="$childs">
                                    <li>
                                        <a href="#">
                                            <xsl:value-of select="@name"/>
                                        </a>
                                    </li>
                                </xsl:for-each>
                            </ul>
                        </xsl:if>
                    </li>
                </ul>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    在输入中,child2与输出中未出现的不同子项(例如bb)一起多次出现。这是一个错误吗?child2是唯一的吗?对不起,输入child2是为了表示深度,但名称是唯一的。我将更新这个问题。您是否打算将密钥作为参数传递给xslt?请参阅我的答案,以了解我认为是您的问题的解决方案。每当你纠正你的问题,我都愿意给它一个+1。我已经编辑了我的问题,希望这能让它更清楚。嘿,谢谢你。所谓直接子节点,我指的是所选节点正下方的级别。因此,如果选择了child-1,它将只显示child2-1、child2-2等。此外,这似乎会输出每个顶级项目,即/toc/itemHi。谢谢。这把独特的钥匙就像一个符咒。如何将子键添加到“select=”key('itemBykey',$key)”中的键?@Barry Jones:你很好。关于你的问题:现在的键是@key加@subkey,因此xsl:key/@用法应该是
    concat(@key,'some-character-not-present-in-key',@subkey)
    ,而
    key()
    函数应该像
    key一样('itemBykey',concat('key,'some-character-not-present-in-keys',$subkey))
    和$subkey添加了一个新参数。很抱歉再次询问,但这里有一点小麻烦。我使用了:key('itemBykey',concat('key,'-',$subkey)),但即使在select和select:key防御上使用它,它也不起作用('itemBykey',concat($id',-',$subId)),其中$id和$subId声明为:@Barry Jones:检查您的姓名(在您的示例中是@subkey而不是@subkey)和输入。这些更改对我有效。