温馨提示:本文翻译自stackoverflow.com,查看原文请点击:xslt - Using key() and count() to calculate the number of episode wich only contain guest and are female or

xslt - 使用key()和count()计算仅包含来宾且为女性或女性的情节数目

发布于 2020-03-29 22:08:32

假设我要计算剧集数。详细地说,我想获取仅包含男女作为嘉宾的剧集数量。我希望我的问题是明确的,并且有些合理。

输入:

<pc:podcast url="https://forschergeist.de/" version="0.3" xmlns:pc="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3" xmlns:pt="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="   https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3   podcast.xsd   https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1   podcast-transcript.xsd">
    <pc:title>Forschergeist</pc:title>
    <pc:persons>
        <pc:person gender="male" pid="timpritlove">Tim Pritlove</pc:person>
        <pc:person gender="male" pid="michaelsonnabend">Michael Sonnabend</pc:person>
        <pc:person gender="female" pid="annettevogt">Annette Vogt</pc:person>
        <pc:person gender="male" pid="peterfinke">Peter Finke</pc:person>
        <pc:person gender="female" pid="barbaralenz">Barbara Lenz</pc:person>
        <pc:person gender="male" pid="suatyilmaz">Suat Yilmaz</pc:person>
        <pc:person gender="male" pid="onurgüntürkün">Onur Güntürkün</pc:person>
        <pc:person gender="male" pid="martinlindner">Martin Lindner</pc:person>
        <pc:person gender="male" pid="petereisenberg">Peter Eisenberg</pc:person>
        <pc:person gender="male" pid="michaelschermann">Michael Schermann</pc:person>
        <pc:person gender="male" pid="henrikmüller">Henrik Müller</pc:person>
        <pc:person gender="female" pid="antjeboetius">Antje Boetius</pc:person>
        <pc:person gender="male" pid="gunterdueck">Gunter Dueck</pc:person>
        <pc:person gender="male" pid="ferdischüth">Ferdi Schüth</pc:person>
        <pc:person gender="female" pid="claudiaeggert">Claudia Eggert</pc:person>
        <pc:person gender="female" pid="dagmarschäfer">Dagmar Schäfer</pc:person>
        <pc:person gender="female" pid="murielhelbig">Muriel Helbig</pc:person>
        <pc:person gender="male" pid="nikolauskriegeskorte">Nikolaus Kriegeskorte</pc:person>
        <pc:person gender="female" pid="ulinkarublak">Ulinka Rublack</pc:person>
        <pc:person gender="male" pid="michaelbraungart">Michael Braungart</pc:person>
        <pc:person gender="male" pid="pascalhetze">Pascal Hetze</pc:person>
        <pc:person gender="female" pid="lindabreitlauch">Linda Breitlauch</pc:person>
        <pc:person gender="male" pid="jürgenhandke">Jürgen Handke</pc:person>
        <pc:person gender="male" pid="franzjosefradermacher">Franz Josef Radermacher</pc:person>
        <pc:person gender="male" pid="klauskümmerer">Klaus Kümmerer</pc:person>
        <pc:person gender="male" pid="uweschneidewind">Uwe Schneidewind</pc:person>
        <pc:person gender="male" pid="eberhardknobloch">Eberhard Knobloch</pc:person>
        <pc:person gender="male" pid="ulrichdirnagl">Ulrich Dirnagl</pc:person>
        <pc:person gender="male" pid="hansjoosten">Hans Joosten</pc:person>
        <pc:person gender="female" pid="angelikabrandt">Angelika Brandt</pc:person>
        <pc:person gender="male" pid="andreaszick">Andreas Zick</pc:person>
        <pc:person gender="male" pid="güntherziegler">Günther Ziegler</pc:person>
        <pc:person gender="male" pid="michaelvogel">Michael Vogel</pc:person>
        <pc:person gender="female" pid="siljagraupe">Silja Graupe</pc:person>
        <pc:person gender="male" pid="ernstpeterfischer">Ernst Peter Fischer</pc:person>
        <pc:person gender="female" pid="juliaoffe">Julia Offe</pc:person>
        <pc:person gender="male" pid="pavelrichter">Pavel Richter</pc:person>
        <pc:person gender="male" pid="benjaminlahusen">Benjamin Lahusen</pc:person>
        <pc:person gender="male" pid="reinholdleinfelder">Reinhold Leinfelder</pc:person>
        <pc:person gender="female" pid="claudiahornberg">Claudia Hornberg</pc:person>
        <pc:person gender="female" pid="monikatrümper">Monika Trümper</pc:person>
        <pc:person gender="male" pid="jörnloviscach">Jörn Loviscach</pc:person>
        <pc:person gender="female" pid="lisarosa">Lisa Rosa</pc:person>
        <pc:person gender="male" pid="ralfstockmann">Ralf Stockmann</pc:person>
        <pc:person gender="female" pid="lyndalroper">Lyndal Roper</pc:person>
        <pc:person gender="male" pid="jürgentautz">Jürgen Tautz</pc:person>
        <pc:person gender="male" pid="stefankröpelin">Stefan Kröpelin</pc:person>
        <pc:person gender="male" pid="christophcorves">Christoph Corves</pc:person>
        <pc:person gender="male" pid="jöranmuußmerholz">Jöran Muußmerholz</pc:person>
        <pc:person gender="male" pid="hannshatt">Hanns Hatt</pc:person>
        <pc:person gender="female" pid="carolinbock">Carolin Bock</pc:person>
        <pc:person gender="female" pid="andreaniehaus">Andrea Niehaus</pc:person>
        <pc:person gender="male" pid="volkerquaschning">Volker Quaschning</pc:person>
        <pc:person gender="male" pid="jochenrabe">Jochen Rabe</pc:person>
        <pc:person gender="male" pid="haraldlesch">Harald Lesch</pc:person>
        <pc:person gender="male" pid="sebastiankuhn">Sebastian Kuhn</pc:person>
        <pc:person gender="male" pid="manfredniekisch">Manfred Niekisch</pc:person>
        <pc:person gender="male" pid="christopherzimmermann">Christopher Zimmermann</pc:person>
        <pc:person gender="male" pid="philippewampfler">Philippe Wampfler</pc:person>
        <pc:person gender="male" pid="stefanrahmstorf">Stefan Rahmstorf</pc:person>
        <pc:person gender="female" pid="julespecht">Jule Specht</pc:person>
        <pc:person gender="female" pid="isabelrink">Isabel Rink</pc:person>
        <pc:person gender="male" pid="ingosass">Ingo Sass</pc:person>
        <pc:person gender="male" pid="robertranisch">Robert Ranisch</pc:person>
        <pc:person gender="female" pid="anitagohdes">Anita Gohdes</pc:person>
        <pc:person gender="male" pid="gerdgigerenzer">Gerd Gigerenzer</pc:person>
        <pc:person gender="female" pid="alinakokoschka">Alina Kokoschka</pc:person>
        <pc:person gender="male" pid="oliverparodi">Oliver Parodi</pc:person>
        <pc:person gender="male" pid="justushaukap">Justus Haukap</pc:person>
        <pc:person gender="male" pid="peterpurgathofer">Peter Purgathofer</pc:person>
    </pc:persons>
<pc:episode date="2019-10-01T11:00:00" duration="01:53:23" episode="72" title="Verantwortung in der Informatik" url="https://forschergeist.de/podcast/fg072-verantwortung-in-der-informatik/">
        <pc:contributor pid="timpritlove" role="host"/>
        <pc:contributor pid="peterpurgathofer" role="guest"/>

</pc:episode>

<pc:episode date="2019-09-05T14:48:00" duration="02:29:05" episode="71" title="Wirtschaft und Wettbewerb" url="https://forschergeist.de/podcast/fg071-wirtschaft-und-wettbewerb/">
    <pc:contributor pid="timpritlove" role="host"/>
    <pc:contributor pid="justushaucap" role="guest"/>
</pc:episode>
<pc:episode date="2019-07-29T11:00:00" duration="01:28:03" episode="70" title="Nachhaltigkeit und die Stadt" url="https://forschergeist.de/podcast/fg070-nachhaltigkeit-und-die-stadt/">
        <pc:contributor pid="timpritlove" role="host"/>
        <pc:contributor pid="oliverparodi" role="guest"/>



</pc:episode>



<pc:episode date="2020-01-01T00:00:00" duration="0" episode="043" title="Schule und Lernen in der digitalen Welt" url="https://forschergeist.de/podcast/fg043-schule-und-lernen-in-der-digitalen-welt/">
        <pc:contributor pid="timpritlove" role="host"/>
        <pc:contributor pid="lisarosa" role="guest"/>
</pc:epsiode>

我的xsl:

<?xml version="1.0"?>

    <xsl:stylesheet version="1.0" 
        xmlns:pc="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3" 
        xmlns:pt="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xhtml"/>



        <xsl:key name="person-name" match="pc:person" use="@pid"/>
        <xsl:key name="geschlecht" match="pc:person" use="@gender"/>
        <xsl:template match="pc:podcast">
            <html>
                <body>
                    <h1>Episoden, in welchen der Guest Männlich ode weiblich war.<i><xsl:value-of select="pc:title"/></i></h1>
                    <ul>
                        <xsl:apply-templates select="pc:episode" />
                    </ul>
                </body>
            </html>
        </xsl:template>

        <xsl:template match="pc:episode">
            <li>
                <xsl:value-of select="@episode" />: <xsl:value-of select="@title" />
                <ul>
                    <xsl:apply-templates select="pc:contributor" />
                </ul>
            </li>
        </xsl:template>

        <xsl:template match="pc:contributor">

                <xsl:for-each select="key('person-name', @pid,)">
                    <li>
                    <xsl:value-of select="."/>
                </li>
                </xsl:for-each>
                <!-- <xsl:value-of select="key('person-name', @pid)"/>   -->
        </xsl:template>




    </xsl:stylesheet>

所需输出:

4 episode where guest is male
1 Episode where guest is female 

如何正确使用key()和count()函数?在此先感谢您回答我的问题。

查看更多

提问者
ASCIIIIIII
被浏览
23
michael.hor257k 2020-01-31 21:04

考虑以下示例:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pc="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3" 
exclude-result-prefixes="pc">
<xsl:output method="html"/>

<xsl:key name="person-by-id" match="pc:person" use="@pid"/>

<xsl:template match="/pc:podcast">
    <html>
        <body>
            <p>
                <xsl:value-of select="count(pc:episode[key('person-by-id', pc:contributor[@role='guest']/@pid)/@gender='male'])"/>
                <xsl:text> episodes with male guest</xsl:text>
            </p>
            <p>
                <xsl:value-of select="count(pc:episode[key('person-by-id', pc:contributor[@role='guest']/@pid)/@gender='female'])"/>
                <xsl:text> episodes with female guest</xsl:text>
            </p>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

应用于格式正确的(!)输入:

XML格式

<?xml version="1.0" encoding="UTF-8"?>
<pc:podcast xmlns:pc="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3" xmlns:pt="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" url="https://forschergeist.de/" version="0.3" xsi:schemaLocation="       https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3     podcast.xsd     https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1   podcast-transcript.xsd">
    <pc:title>Forschergeist</pc:title>
    <pc:persons>
        <pc:person gender="male" pid="timpritlove">Tim Pritlove</pc:person>
        <pc:person gender="male" pid="michaelsonnabend">Michael Sonnabend</pc:person>
        <pc:person gender="female" pid="annettevogt">Annette Vogt</pc:person>
        <pc:person gender="male" pid="peterfinke">Peter Finke</pc:person>
        <pc:person gender="female" pid="barbaralenz">Barbara Lenz</pc:person>
        <pc:person gender="male" pid="suatyilmaz">Suat Yilmaz</pc:person>
        <pc:person gender="male" pid="onurgüntürkün">Onur Güntürkün</pc:person>
        <pc:person gender="male" pid="martinlindner">Martin Lindner</pc:person>
        <pc:person gender="male" pid="petereisenberg">Peter Eisenberg</pc:person>
        <pc:person gender="male" pid="michaelschermann">Michael Schermann</pc:person>
        <pc:person gender="male" pid="henrikmüller">Henrik Müller</pc:person>
        <pc:person gender="female" pid="antjeboetius">Antje Boetius</pc:person>
        <pc:person gender="male" pid="gunterdueck">Gunter Dueck</pc:person>
        <pc:person gender="male" pid="ferdischüth">Ferdi Schüth</pc:person>
        <pc:person gender="female" pid="claudiaeggert">Claudia Eggert</pc:person>
        <pc:person gender="female" pid="dagmarschäfer">Dagmar Schäfer</pc:person>
        <pc:person gender="female" pid="murielhelbig">Muriel Helbig</pc:person>
        <pc:person gender="male" pid="nikolauskriegeskorte">Nikolaus Kriegeskorte</pc:person>
        <pc:person gender="female" pid="ulinkarublak">Ulinka Rublack</pc:person>
        <pc:person gender="male" pid="michaelbraungart">Michael Braungart</pc:person>
        <pc:person gender="male" pid="pascalhetze">Pascal Hetze</pc:person>
        <pc:person gender="female" pid="lindabreitlauch">Linda Breitlauch</pc:person>
        <pc:person gender="male" pid="jürgenhandke">Jürgen Handke</pc:person>
        <pc:person gender="male" pid="franzjosefradermacher">Franz Josef Radermacher</pc:person>
        <pc:person gender="male" pid="klauskümmerer">Klaus Kümmerer</pc:person>
        <pc:person gender="male" pid="uweschneidewind">Uwe Schneidewind</pc:person>
        <pc:person gender="male" pid="eberhardknobloch">Eberhard Knobloch</pc:person>
        <pc:person gender="male" pid="ulrichdirnagl">Ulrich Dirnagl</pc:person>
        <pc:person gender="male" pid="hansjoosten">Hans Joosten</pc:person>
        <pc:person gender="female" pid="angelikabrandt">Angelika Brandt</pc:person>
        <pc:person gender="male" pid="andreaszick">Andreas Zick</pc:person>
        <pc:person gender="male" pid="güntherziegler">Günther Ziegler</pc:person>
        <pc:person gender="male" pid="michaelvogel">Michael Vogel</pc:person>
        <pc:person gender="female" pid="siljagraupe">Silja Graupe</pc:person>
        <pc:person gender="male" pid="ernstpeterfischer">Ernst Peter Fischer</pc:person>
        <pc:person gender="female" pid="juliaoffe">Julia Offe</pc:person>
        <pc:person gender="male" pid="pavelrichter">Pavel Richter</pc:person>
        <pc:person gender="male" pid="benjaminlahusen">Benjamin Lahusen</pc:person>
        <pc:person gender="male" pid="reinholdleinfelder">Reinhold Leinfelder</pc:person>
        <pc:person gender="female" pid="claudiahornberg">Claudia Hornberg</pc:person>
        <pc:person gender="female" pid="monikatrümper">Monika Trümper</pc:person>
        <pc:person gender="male" pid="jörnloviscach">Jörn Loviscach</pc:person>
        <pc:person gender="female" pid="lisarosa">Lisa Rosa</pc:person>
        <pc:person gender="male" pid="ralfstockmann">Ralf Stockmann</pc:person>
        <pc:person gender="female" pid="lyndalroper">Lyndal Roper</pc:person>
        <pc:person gender="male" pid="jürgentautz">Jürgen Tautz</pc:person>
        <pc:person gender="male" pid="stefankröpelin">Stefan Kröpelin</pc:person>
        <pc:person gender="male" pid="christophcorves">Christoph Corves</pc:person>
        <pc:person gender="male" pid="jöranmuußmerholz">Jöran Muußmerholz</pc:person>
        <pc:person gender="male" pid="hannshatt">Hanns Hatt</pc:person>
        <pc:person gender="female" pid="carolinbock">Carolin Bock</pc:person>
        <pc:person gender="female" pid="andreaniehaus">Andrea Niehaus</pc:person>
        <pc:person gender="male" pid="volkerquaschning">Volker Quaschning</pc:person>
        <pc:person gender="male" pid="jochenrabe">Jochen Rabe</pc:person>
        <pc:person gender="male" pid="haraldlesch">Harald Lesch</pc:person>
        <pc:person gender="male" pid="sebastiankuhn">Sebastian Kuhn</pc:person>
        <pc:person gender="male" pid="manfredniekisch">Manfred Niekisch</pc:person>
        <pc:person gender="male" pid="christopherzimmermann">Christopher Zimmermann</pc:person>
        <pc:person gender="male" pid="philippewampfler">Philippe Wampfler</pc:person>
        <pc:person gender="male" pid="stefanrahmstorf">Stefan Rahmstorf</pc:person>
        <pc:person gender="female" pid="julespecht">Jule Specht</pc:person>
        <pc:person gender="female" pid="isabelrink">Isabel Rink</pc:person>
        <pc:person gender="male" pid="ingosass">Ingo Sass</pc:person>
        <pc:person gender="male" pid="robertranisch">Robert Ranisch</pc:person>
        <pc:person gender="female" pid="anitagohdes">Anita Gohdes</pc:person>
        <pc:person gender="male" pid="gerdgigerenzer">Gerd Gigerenzer</pc:person>
        <pc:person gender="female" pid="alinakokoschka">Alina Kokoschka</pc:person>
        <pc:person gender="male" pid="oliverparodi">Oliver Parodi</pc:person>
        <pc:person gender="male" pid="justushaukap">Justus Haukap</pc:person>
        <pc:person gender="male" pid="peterpurgathofer">Peter Purgathofer</pc:person>
    </pc:persons>
    <pc:episode date="2019-10-01T11:00:00" duration="01:53:23" episode="72" title="Verantwortung in der Informatik" url="https://forschergeist.de/podcast/fg072-verantwortung-in-der-informatik/">
        <pc:contributor pid="timpritlove" role="host"/>
        <pc:contributor pid="peterpurgathofer" role="guest"/>
    </pc:episode>
    <pc:episode date="2019-09-05T14:48:00" duration="02:29:05" episode="71" title="Wirtschaft und Wettbewerb" url="https://forschergeist.de/podcast/fg071-wirtschaft-und-wettbewerb/">
        <pc:contributor pid="timpritlove" role="host"/>
        <pc:contributor pid="justushaucap" role="guest"/>
    </pc:episode>
    <pc:episode date="2019-07-29T11:00:00" duration="01:28:03" episode="70" title="Nachhaltigkeit und die Stadt" url="https://forschergeist.de/podcast/fg070-nachhaltigkeit-und-die-stadt/">
        <pc:contributor pid="timpritlove" role="host"/>
        <pc:contributor pid="oliverparodi" role="guest"/>
    </pc:episode>
    <pc:episode date="2020-01-01T00:00:00" duration="0" episode="043" title="Schule und Lernen in der digitalen Welt" url="https://forschergeist.de/podcast/fg043-schule-und-lernen-in-der-digitalen-welt/">
        <pc:contributor pid="timpritlove" role="host"/>
        <pc:contributor pid="lisarosa" role="guest"/>
    </pc:episode>
</pc:podcast>

结果将是:

<html>
   <body>
      <p>2 episodes with male guest</p>
      <p>1 episodes with female guest</p>
   </body>
</html>

请注意,您的XML结构允许每个情节有多个来宾。样式表计算至少一位客人是男性还是女性的情节。