搜索最大层次结构 [英] search for maximum hierarchy

查看:75
本文介绍了搜索最大层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,


可能有些人可以帮助我解决一个XSLT示例如何解决以下挑战的



对于下面的XML,我想找出我的XSLT中

特定元素的最大层次结构级别。该示例的结果(搜索

< A />)应为4,因为元素A最多嵌套4次。

我想我必须使用不知怎的,计数函数带有''跟随:: A''

轴。但是我还没办法工作。


非常感谢你的帮助

Rolf


< ; ROOT>

< A>

< A>

< B>

< C> ;

< A />最多嵌套3

< / C>

< / B>

< / A>

< A>

< B>

< C>

< A>

< ; A />最多嵌套4

< / A>

< C>

< / B>

< / A>

< / A>

< / ROOT>

解决方案

< blockquote> Ke*****@t-online.de (KemperR)写道:

可能有些人可以帮我解决一下XSLT示例如何解决后面的问题。
对于下面的XML,我想找出
特定元素的最大层次结构级别在我的XSLT中。该示例的结果(搜索
< A />)应为4,因为元素A最多嵌套4次。
我想我必须以某种方式使用计数函数''跟随:: A''
轴。但是我还没办法工作。




嗯。我觉得这件事非常棘手。该解决方案看起来很复杂,但它的大部分只是一个递归模板,用于查找

数字列表的最大值。


这样做的工作:


---- XML ----(你的xml已更正)

< ROOT>

< A>

< A>

< B>

< C>

< A />

< / C>

< / B>

< / A>

< A>

< B>

< C>

< A>

< A />

< / A>

< / C>

< / B>

< / A>

< / A>

< / ROOT>


- --- XSL ---

< xsl:stylesheet

version =" 1.0"

xmlns:xsl =" http ://www.w3.org/1999/XSL/Transform">


< xsl:template match =" /">

< xsl:call-template name =" max">

< xsl:with-param name =" data& >

<! - 我们构建一个局部最大树深度列表 - >

< xsl:for-each select =" ; // * [not(child :: *)]">

< xsl:value-of select =" count(ancestor :: *)" />

< xsl:text>:< / xsl:text>

< / xsl:for-each>

< / xsl: with-param>

< xsl:with-param name =" max" select =" 0" />

< / xsl:call-template>

< / xsl:template>


< xsl:template name =" max">

< xsl:param name =" data" />

< xsl: param name =" max" />

< xsl:choose>

< xsl:when test ="


< blockquote> data">

< xsl:variable name =" current"

select =" number(substring-before(


data,'':''))" />

< xsl:call-template name =" max">

< ; xsl:with-param name =" data"

select =" substring-after(


Dear All,

may be some of you can help me with an XSLT example how to solve the
following challange.
For the XML below I want to find out the maximum hierarchy level for a
specific element in my XSLT. The result for the example (searching for
<A/>) should be 4 as the element A is nested 4 times maximum.
I guess I have to use somehow the count function with ''following::A''
axes. But I could not get that to work yet.

Thanks a lot for your help
Rolf

<ROOT>
<A>
<A>
<B>
<C>
<A/> nested up to 3
</C>
</B>
</A>
<A>
<B>
<C>
<A>
<A/> nested up to 4
</A>
<C>
</B>
</A>
</A>
</ROOT>

解决方案

Ke*****@t-online.de (KemperR) writes:

may be some of you can help me with an XSLT example how to solve the
following challange.
For the XML below I want to find out the maximum hierarchy level for a
specific element in my XSLT. The result for the example (searching for
<A/>) should be 4 as the element A is nested 4 times maximum.
I guess I have to use somehow the count function with ''following::A''
axes. But I could not get that to work yet.



Hmmm. I found this surprisingly tricky to do. The solution looks
complex, but the bulk of it is just a recursive template for finding
the maximum of a list of numbers.

This does the job:

----XML---- (your xml corrected)
<ROOT>
<A>
<A>
<B>
<C>
<A/>
</C>
</B>
</A>
<A>
<B>
<C>
<A>
<A/>
</A>
</C>
</B>
</A>
</A>
</ROOT>

----XSL---
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:call-template name="max">
<xsl:with-param name="data">
<!-- We construct a list of local-maximum tree-depths -->
<xsl:for-each select="//*[not(child::*)]">
<xsl:value-of select="count(ancestor::*)"/>
<xsl:text>:</xsl:text>
</xsl:for-each>
</xsl:with-param>
<xsl:with-param name="max" select="0"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="max">
<xsl:param name="data"/>
<xsl:param name="max"/>
<xsl:choose>
<xsl:when test="


data">
<xsl:variable name="current"
select="number(substring-before(


data,'':''))"/>
<xsl:call-template name="max">
<xsl:with-param name="data"
select="substring-after(


这篇关于搜索最大层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆