执行“分组依据” XPath XSL 中的查询
给定以下 XML:
<results name="queryResults">
<int name="intfield1:[* TO 10]">11</int>
<int name="intfield2:[10 TO 20]">9</int>
<int name="intfield1:[10 TO 20]">12</int>
</results>
我想生成此 XML:
<results>
<field name="numberfield1">
<value name="[* TO 10]">11</value>
<value name="[10 TO 10]">12</value>
</field>
<field name="numberfield2">
<value name="[10 TO 20]">9</value>
</field>
</results>
我不知道如何在 XSL 中执行此操作,主要是因为我想按数字字段进行分组。我能想到的是:
<xsl:if test="count(results/int) > 0">
<results>
<xsl:for-each select="results/int">
<field>
<xsl:attribute name="name">
<xsl:value-of select="substring-before(@name, ':')"/></xsl:attribute>
<value>
<xsl:attribute name="name">
<xsl:value-of select="substring-after(@name, ':') "/>
</xsl:attribute>
<xsl:value-of select="."/>
</value>
</field>
</xsl:for-each>
</results>
</xsl:if>
但是这并不'不产生很好的分组列表,而是我得到这个:
<results>
<field name="numberfield1">
<value name="[* TO 10]">11</value>
</field>
<field name="numberfield2">
<value name="[10 TO 20]">9</value>
</field>
<field name="numberfield1">
<value name="[10 TO 10]">12</value>
</field>
</results>
如果有人可以引导我朝正确的方向前进..那就太好了?
谢谢
Given the following XML:
<results name="queryResults">
<int name="intfield1:[* TO 10]">11</int>
<int name="intfield2:[10 TO 20]">9</int>
<int name="intfield1:[10 TO 20]">12</int>
</results>
I would like to produce this XML:
<results>
<field name="numberfield1">
<value name="[* TO 10]">11</value>
<value name="[10 TO 10]">12</value>
</field>
<field name="numberfield2">
<value name="[10 TO 20]">9</value>
</field>
</results>
I can't think how to do this in XSL mainly because i'm wanting to Group by the numbericfield.. All i can come up with is this:
<xsl:if test="count(results/int) > 0">
<results>
<xsl:for-each select="results/int">
<field>
<xsl:attribute name="name">
<xsl:value-of select="substring-before(@name, ':')"/></xsl:attribute>
<value>
<xsl:attribute name="name">
<xsl:value-of select="substring-after(@name, ':') "/>
</xsl:attribute>
<xsl:value-of select="."/>
</value>
</field>
</xsl:for-each>
</results>
</xsl:if>
However this doesn't produce the nice grouped list instead i get this:
<results>
<field name="numberfield1">
<value name="[* TO 10]">11</value>
</field>
<field name="numberfield2">
<value name="[10 TO 20]">9</value>
</field>
<field name="numberfield1">
<value name="[10 TO 10]">12</value>
</field>
</results>
If someone can stear me in the right direction.. That would be great?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在 XSLT 1.0 中执行此操作,您必须使用一种名为 "muenchian grouping"< 的技术/a>.首先创建要分组的节点的键
接下来,迭代所有节点,但仅选择相关组中恰好位于第一个的节点
接下来,您可以使用该键迭代所有节点在组中将
所有这些放在一起给出
在您的示例中,“intfield”变为“numberfield”。在上面的示例中,我将名称保留为“intfield”。
To do this in XSLT 1.0, you will have to use a technique called "muenchian grouping". First create a key of the nodes on which you wish to group
Next, you iterate it through all the nodes, but only selecting the ones that happen to be first in the relevant group
Next, you can iterate use the key to iterate over all nodes in the group
Putting this all together gives
In your example, 'intfield' becomes 'numberfield' though. I have kept the name as 'intfield' in the above example.
慕尼黑的分组是天才的杰作。不太容易理解,但请看:
http://www.jenitennison.com/xslt/grouping/muenchian.html
为了简化流程,W3C 特别支持 XSLT2.0 中的分组。例如,参见:
http://www.xml.com/pub/a/ 2003/11/05/tr.html
然而并非所有环境都支持 XSLT2.0
Muenchian grouping is a work of genius. It's not easy to understand, but see:
http://www.jenitennison.com/xslt/grouping/muenchian.html
To simplify the process the W3C specifically supported grouping in XSLT2.0. See, for example:
http://www.xml.com/pub/a/2003/11/05/tr.html
However not all environments support XSLT2.0