XSLT:使用键和条件的“选择值"? [英] XSLT: 'value-of select' using keys and conditions?

查看:25
本文介绍了XSLT:使用键和条件的“选择值"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自上一个线程:XSLT: 根据来自其他节点的值的总和进行排序

this question follows on from previous thread: XSLT: Sorting based on sum of values from other nodes

我现在可以使用密钥从其他节点获取我的数据总和.我似乎无法得到的是现在应用条件来选择我在使用这些键时所需要的数据所需的语法或方法.

I can now get my data summed from the other nodes by using keys. What I can't seem to get is the syntax or method needed to now apply conditions to select the data I'm after while using these keys.

这是我正在使用的简化 xml(从上一个修改以突出问题):

Here is a simplified xml I'm using (modified from last one to highlight issues):

<Horses>
    <Horse>
        <ID>1</ID>
        <Name>hrsA</Name>
        <SireID>101</SireID>
        <Age>3</Age>
        <Pace>
            <Stakes>20</Stakes>
            <Wins>0</Wins>
        </Pace>
    </Horse>
    <Horse>
        <ID>2</ID>
        <Name>hrsB</Name>
        <SireID>101</SireID>
        <Age>6</Age>
        <Pace>
            <Stakes>1600</Stakes>
            <Wins>9</Wins>
        </Pace>
    </Horse>
    <Horse>
        <ID>3</ID>
        <Name>hrsC</Name>
        <SireID>101</SireID>
        <Age>3</Age>
        <Trot>
            <Stakes>200</Stakes>
            <Wins>2</Wins>
        </Trot>
    </Horse>
    <Horse>
        <ID>4</ID>
        <Name>hrsD</Name>
        <SireID>101</SireID>
        <Age>4</Age>
        <Pace>
            <Stakes>50</Stakes>
            <Wins>0</Wins>
        </Pace>
        <Trot>
            <Stakes>100</Stakes>
            <Wins>1</Wins>
        </Trot>
    </Horse>
    <Horse>
        <ID>5</ID>
        <Name>hrsE</Name>
        <SireID>101</SireID>
        <Age>3</Age>
        <Pace>
            <Stakes>100</Stakes>
            <Wins>1</Wins>
        </Pace>
        <Trot>
            <Stakes>300</Stakes>
            <Wins>1</Wins>
        </Trot>
    </Horse>
</Horses>
<Sires>
    <Sire>
        <ID>101</ID>
        <Name>srA</Name>
        <LiveFoalsALL>117</LiveFoalsALL>
    </Sire>
</Sires>

在一种情况下,我需要获得属于某个年龄的某个公爵的后代(马).

In one scenario I need to get the offspring (Horses) belonging to one Sire that are of a certain age.

在没有指定年龄的情况下,我使用了一个对赢得的赌注进行排序的密钥(感谢 Dimitre):

With no age specified I use a key that sorts on stakes won like so (thanks to Dimitre):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kOffspring" match="Horse" use="SireID"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="Sires/Sire">
   <xsl:sort select="sum(key('kOffspring', ID)/*/Stakes)"
             data-type="number" order="descending"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="Sire">
    <!-- get the data I want -->
 </xsl:template>
</xsl:stylesheet>

现在,在获取我想要的数据"中,我试图只获取 3 岁的马(年龄 = 3)例如,为了只获得 srA 3 岁的获胜后代(答案 = 2),我需要这样的东西:

Now, in the 'get the data I want' I'm trying to only get 3 year old horses (Age=3) For instance, to only get srA's 3 year old winning offspring (answer = 2) I need something like this I'm thinking:

<xsl:value-of select="count(key('kOffspring', ID)[Age=3]/*/Wins)"/>

但这不起作用......或者我是否需要为每个我想要使用的年龄创建一个新密钥,并使用一些条件语法(?),例如:

but that does not work... Or do I need to create a new key for each age I want to use with some conditional syntax(?) like:

<xsl:key name="kOffspring" match="Horse[/Age=3]" use="SireID"/>

正如你所看到的,我真的不知道我在做什么,或者是否有可能:-)

As you can see I don't really know what I'm doing or if it is even possible :-)

.

与此类似的另一种情况是需要计算有多少匹马是获胜者 - answer=4(不是获胜次数,只是他们是否以配速或小跑获胜......第三种情况需要仅以配速或小跑获胜)只是在小跑).

The other scenario in a similar vein is needing to count how many horses were winners - answer=4 (not the number of wins, just whether they won at pace or trot ... a third scenario requires either just at pace or just at trot).

我在 Sire 模板中尝试了这个:

I tried this in the Sire template with:

<xsl:value-of select="count(key('kOffspring', ID)/*/Wins &gt; 0)"/>

但那只会返回一个计数.

but that only ever returns a count of one.

如果这里有任何专家可以帮助我前进,我将不胜感激.与其他编程语言相比,我发现 xslt 语法有点混乱且难以记住 - 我希望我能尽快掌握它,因为它非常强大.

If any experts here could help get me going I would appreciate that. I am finding xslt syntax a little confusing and hard to remember compared to other programming languages - I hope I get the swing of it soon as it is very powerful.

问候,布莱斯·斯坦伯格.

Regards, Bryce Stenberg.

推荐答案

这种转变:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kOffspring" match="Horse" use="SireID"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="Sires/Sire">
   <xsl:sort select="sum(key('kOffspring', ID)/*/Stakes)"
             data-type="number" order="descending"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="Sire">
     Sire <xsl:value-of select="concat(ID,' (', Name, ') Stakes: ')"/>
   <xsl:value-of select="sum(key('kOffspring', ID)/*/Stakes)"/>
     3 year old winning offspring: <xsl:value-of
     select="count(key('kOffspring', ID)[Age = 3 and */Wins > 0])"/>
     Offspring that ever were a winner: <xsl:value-of
      select="count(key('kOffspring', ID)[*/Wins > 0])"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML(提供的片段,包含在单个顶部元素中以使其成为格式良好的 XML 文档):

When applied on the provided XML (the provided fragment, enclosed in a single top element to make it a well-formed XML document):

<t>
    <Horses>
        <Horse>
            <ID>1</ID>
            <Name>hrsA</Name>
            <SireID>101</SireID>
            <Age>3</Age>
            <Pace>
                <Stakes>20</Stakes>
                <Wins>0</Wins>
            </Pace>
        </Horse>
        <Horse>
            <ID>2</ID>
            <Name>hrsB</Name>
            <SireID>101</SireID>
            <Age>6</Age>
            <Pace>
                <Stakes>1600</Stakes>
                <Wins>9</Wins>
            </Pace>
        </Horse>
        <Horse>
            <ID>3</ID>
            <Name>hrsC</Name>
            <SireID>101</SireID>
            <Age>3</Age>
            <Trot>
                <Stakes>200</Stakes>
                <Wins>2</Wins>
            </Trot>
        </Horse>
        <Horse>
            <ID>4</ID>
            <Name>hrsD</Name>
            <SireID>101</SireID>
            <Age>4</Age>
            <Pace>
                <Stakes>50</Stakes>
                <Wins>0</Wins>
            </Pace>
            <Trot>
                <Stakes>100</Stakes>
                <Wins>1</Wins>
            </Trot>
        </Horse>
        <Horse>
            <ID>5</ID>
            <Name>hrsE</Name>
            <SireID>101</SireID>
            <Age>3</Age>
            <Pace>
                <Stakes>100</Stakes>
                <Wins>1</Wins>
            </Pace>
            <Trot>
                <Stakes>300</Stakes>
                <Wins>1</Wins>
            </Trot>
        </Horse>
    </Horses>
    <Sires>
        <Sire>
            <ID>101</ID>
            <Name>srA</Name>
            <LiveFoalsALL>117</LiveFoalsALL>
        </Sire>
    </Sires>
</t>

产生想要的、正确的结果:

 Sire 101 (srA) Stakes: 2370
 3 year old winning offspring: 2
 Offspring that ever were a winner: 4

<小时>

或者,一个可能更有效的解决方案是使用另一个复合键,它的两部分是 SireIDAge:


Alternatively, a probably more efficient solution is to use another key that is composite and its two parts are the SireID and the Age:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kOffspring" match="Horse" use="SireID"/>
 <xsl:key name="kOffspringBySireIdAndAge" match="Horse"
  use="concat(SireID, '+', Age)"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="Sires/Sire">
   <xsl:sort select="sum(key('kOffspring', ID)/*/Stakes)"
             data-type="number" order="descending"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="Sire">
     Sire <xsl:value-of select="concat(ID,' (', Name, ') Stakes: ')"/>
   <xsl:value-of select="sum(key('kOffspring', ID)/*/Stakes)"/>
     3 year old winning offspring: <xsl:value-of
     select="count(key('kOffspringBySireIdAndAge', concat(ID, '+3'))
                       [*/Wins > 0]
                  )"/>
     Offspring that ever were a winner: <xsl:value-of
      select="count(key('kOffspring', ID)[*/Wins > 0])"/>
 </xsl:template>
</xsl:stylesheet>

在同一个 XML 文档(上图)上应用此转换时,会产生相同的正确结果:

 Sire 101 (srA) Stakes: 2370
 3 year old winning offspring: 2
 Offspring that ever were a winner: 4

这篇关于XSLT:使用键和条件的“选择值"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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