执行“分组依据” XPath XSL 中的查询

发布于 2024-08-15 18:28:11 字数 1816 浏览 5 评论 0原文

给定以下 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) &gt; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-08-22 18:28:11

要在 XSLT 1.0 中执行此操作,您必须使用一种名为 "muenchian grouping"< 的技术/a>.首先创建要分组的节点的键

<xsl:key name="intfield" match="int" use="substring-before(@name, ':')" />

接下来,迭代所有节点,但仅选择相关组中恰好位于第一个的节点

<xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">

接下来,您可以使用该键迭代所有节点在组中将

<xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
<xsl:for-each select="key('intfield', $intfieldname)">

所有这些放在一起给出

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml"/>
   <xsl:key name="intfield" match="int" use="substring-before(@name, ':')"/>
   <xsl:template match="/results">
      <results>
         <xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">
            <xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
            <field>
               <xsl:attribute name="name">
                  <xsl:value-of select="$intfieldname"/>
               </xsl:attribute>
               <xsl:for-each select="key('intfield', $intfieldname)">
                  <value>
                     <xsl:attribute name="name">
                        <xsl:value-of select="substring-after(@name, ':')"/>
                     </xsl:attribute>
                     <xsl:value-of select="."/>
                  </value>
               </xsl:for-each>
            </field>
         </xsl:for-each>
      </results>
   </xsl:template>
</xsl:stylesheet>

在您的示例中,“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

<xsl:key name="intfield" match="int" use="substring-before(@name, ':')" />

Next, you iterate it through all the nodes, but only selecting the ones that happen to be first in the relevant group

<xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">

Next, you can iterate use the key to iterate over all nodes in the group

<xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
<xsl:for-each select="key('intfield', $intfieldname)">

Putting this all together gives

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml"/>
   <xsl:key name="intfield" match="int" use="substring-before(@name, ':')"/>
   <xsl:template match="/results">
      <results>
         <xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">
            <xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
            <field>
               <xsl:attribute name="name">
                  <xsl:value-of select="$intfieldname"/>
               </xsl:attribute>
               <xsl:for-each select="key('intfield', $intfieldname)">
                  <value>
                     <xsl:attribute name="name">
                        <xsl:value-of select="substring-after(@name, ':')"/>
                     </xsl:attribute>
                     <xsl:value-of select="."/>
                  </value>
               </xsl:for-each>
            </field>
         </xsl:for-each>
      </results>
   </xsl:template>
</xsl:stylesheet>

In your example, 'intfield' becomes 'numberfield' though. I have kept the name as 'intfield' in the above example.

  • fixed typo.
与风相奔跑 2024-08-22 18:28:11

慕尼黑的分组是天才的杰作。不太容易理解,但请看:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文