如何计算类别中的元素数量并将其与 XSLT 中的其他类别进行比较
我有一个问题:-)
我想打印一个 xml 并为其编写一个 xslt。问题是我想用我的 xml 元素构建一个表,但我必须计算每行是否具有相同数量的列,如果不是,我必须添加一个值元素。
我知道一旦设置了变量值就无法更改变量值,但是如何比较进程的类别/表行数量呢? (并添加空行)
XML:
<Settings>
...
..
<DashBoard>
<Category NAME="ph" PICNAME="prh">
<Process NAME="pd" URL="" PICNAME="prh_prd" />
<Process NAME="md" URL="" PICNAME="prh_prd" />
<Process NAME="t" URL="" PICNAME="prh_prd" />
</Category>
<Category NAME="cm" PICNAME="cam">
<Process NAME="ps" URL="" PICNAME="cam_pls" />
<Process NAME="ea" URL="" PICNAME="cam_eas" />
</Category>
<Category NAME="sm" PICNAME="sum">
<Process NAME="frm" URL="" PICNAME="sum_frm" />
</Category>
</DashBoard>
</Settings>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl=".....">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="relurl" select="/Settings/Picture/@RELATIVEURL"/>
<xsl:template match="Settings">
<table id="dashframe" >
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="Category">
<xsl:variable name="altname" select="@NAME" />
<xsl:variable name="picname" select="@PICNAME" />
<tr>
<th>
<img alt="{$altname}" src="{$relurl}dash_{$picname}_p_01.png" />
</th>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="Process">
<xsl:variable name="altname" select="@NAME" />
<xsl:variable name="picname" select="@PICNAME" />
<td>
<img alt="{$altname}" src="{$relurl}dash_{$picname}_p_01.png" />
</td>
</xsl:template>
</xsl:stylesheet>
所需的输出:
<table id="dashframe" >
<tr>
<th>titel 1</th>
<td>....</td>
<td>....</td>
<td>....</td>
</tr>
<tr>
<th>titel 2</th>
<td>....</td>
<td>....</td>
<td></td>
</tr>
<tr>
<th>titel 3</th>
<td>....</td>
<td></td>
<td></td>
</tr>
</table>
i have a problem :-)
i would like to print out a xml and write a xslt for it. the problem is that i would like to build a table with my xml-elements but i must count if each row has the same amount of columns and if not i have to add a value element.
i know that i cant change a variables value once i have set the value but how can i compare the categorys/table-rows amount of processes then? (and add empty rows)
XML:
<Settings>
...
..
<DashBoard>
<Category NAME="ph" PICNAME="prh">
<Process NAME="pd" URL="" PICNAME="prh_prd" />
<Process NAME="md" URL="" PICNAME="prh_prd" />
<Process NAME="t" URL="" PICNAME="prh_prd" />
</Category>
<Category NAME="cm" PICNAME="cam">
<Process NAME="ps" URL="" PICNAME="cam_pls" />
<Process NAME="ea" URL="" PICNAME="cam_eas" />
</Category>
<Category NAME="sm" PICNAME="sum">
<Process NAME="frm" URL="" PICNAME="sum_frm" />
</Category>
</DashBoard>
</Settings>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl=".....">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="relurl" select="/Settings/Picture/@RELATIVEURL"/>
<xsl:template match="Settings">
<table id="dashframe" >
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="Category">
<xsl:variable name="altname" select="@NAME" />
<xsl:variable name="picname" select="@PICNAME" />
<tr>
<th>
<img alt="{$altname}" src="{$relurl}dash_{$picname}_p_01.png" />
</th>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="Process">
<xsl:variable name="altname" select="@NAME" />
<xsl:variable name="picname" select="@PICNAME" />
<td>
<img alt="{$altname}" src="{$relurl}dash_{$picname}_p_01.png" />
</td>
</xsl:template>
</xsl:stylesheet>
Desired output:
<table id="dashframe" >
<tr>
<th>titel 1</th>
<td>....</td>
<td>....</td>
<td>....</td>
</tr>
<tr>
<th>titel 2</th>
<td>....</td>
<td>....</td>
<td></td>
</tr>
<tr>
<th>titel 3</th>
<td>....</td>
<td></td>
<td></td>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保留您的样式表:
使用此输入:
输出:
注意:众所周知的最大惯用语。
Preserving yours, this stylesheet:
With this input:
Output:
Note: Well known maximum idiom.
感谢您的回答,我在过去 4-5 小时内自己解决了这个问题 ~_~
首先,获取计数:
第二,添加进程,然后添加空进程:
thank´s for your answers, i have solved it myself in the last 4-5 hours ~_~
first, get the count:
second, add the Processes and after this add the empty ones:
因此,您应该分两步完成此操作:
所以第一部分是这里的核心问题,因为仅通过 xpath 不可能找到它。
此问题的答案确认:
查找最大子计数XPath 1.0
我现在尝试用大约 30 分钟来回答这个问题。由于我的工作原因,我对 XML 和 XSLT 非常熟悉。但我无法弄清楚如何找到具有最大子元素数的元素。但其他人之前做过:
找到所有子元素的最大值并在 XSLT 中获取其父元素
顺便说一句:这个人值得对他的答案投赞成票!
如果您有,我们将进入步骤 2:
在您的类别模板中,只需添加对以下模板的调用(这只是一个 for 循环):
现在,您可以调用该模板,简单地填写
count(Process)
作为参数 i,并将计数填写到之前保存的“最大值” - 从链接问题的第一个模板中获得的节点count(maximumnode/Process)
作为参数计数。叹息似乎您遇到了 XSLT 有点困难的地方。
希望有帮助!
So you should do this in 2 Steps:
So the first part is what's the core problem here because it is not possible to find this out just by xpath.
The Answers to this questions confirmed that:
Find the maximum child count with XPath 1.0
I'm trying to answer this question now for about 30 mins. And i am really familiar to XML and XSLT due to my job. But i could't just figure out how to find the element with the maximum count of child elements. But someone else did before:
Find maximum value of all child elements and get its parent element in XSLT
btw: this guy diserves an upvote on his answer!
If you have that we come to step 2:
In your Category template just add a call to the following template (which is just a for loop):
Now then you call that template simpy fill in the
count(Process)
as param i and fill in the count to your previously saved "Maximum" - Node which you got from the first template out of the linked questioncount(maximumnode/Process)
as param count.Sigh seems that you hit some points where XSLT is kind of hard.
Hope that helps!