【问题标题】:xslt counting different content of the same contentxslt计算相同内容的不同内容
【发布时间】:2012-10-20 07:57:04
【问题描述】:

刚开始学习xslt。只是想知道如何计算国际球员的数量?另一件事是国际球员的平均身高?

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="soccer.xslt"?>

    <footballclub>
        <player>
            <based>international</based>
            <height>5.5</height >
            <build>medium</build>
            <age>24</age>
        </player>
        <player>
            <based>local</based>
            <height>5.5</height >
            <build>medium</build>
            <age>24</age>
        </player>
        <player>
            <based>international</based>
            <height>5.5</height >
            <build>medium</build>
            <age>24</age>
        </player>
        <player>
            <based>local</based>
            <height>5.5</height >
            <build>medium</build>
            <age>24</age>
        </player>
    </footballclub>

已尝试以下方法;

count(//football/player/based[not(following::based='international')]) xslt:

<?xml version='1.0' encoding="utf-8"?>
<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" />

<xsl:template match="/">
 <html>
 <head >
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>bombers FC</title>
</head>
<body>
NUMBER OF INTERNATIONAL PLAYERS IS:<xsl:value-of select="count(//football/player/based[not(following::based='international')])"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

结果应为: 国际选手人数:2

【问题讨论】:

    标签: xslt xslt-1.0


    【解决方案1】:

    您的 XPath 需要稍作更改...

    XML 输入(已修正为格式正确)

    <footballclub>
        <player>
            <based>international</based>
            <height>5.5</height>
            <build>medium</build>
            <age>24</age>
        </player>
        <player>
            <based>local</based>
            <height>5.5</height>
            <build>medium</build>
            <age>24</age>
        </player>
        <player>
            <based>international</based>
            <height>5.5</height>
            <build>medium</build>
            <age>24</age>
        </player>
        <player>
            <based>local</based>
            <height>5.5</height>
            <build>medium</build>
            <age>24</age>
        </player>
    </footballclub>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" version="4.0"/>
    
        <xsl:template match="/">
            <html>
                <head>
                    <title>bombers FC</title>
                </head>
                <body>
                    <p>
                        <xsl:text>NUMBER OF INTERNATIONAL PLAYERS IS: </xsl:text>
                        <xsl:value-of select="count(footballclub/player[based='international'])"/>
                    </p>
                    <p>
                        <xsl:text>AVERAGE HEIGHT OF INTERNATIONAL PLAYERS IS: </xsl:text>
                        <xsl:value-of select="sum(footballclub/player[based='international']/height) div count(footballclub/player[based='international'])"/>
                    </p>
                </body>
            </html>
        </xsl:template>
    </xsl:stylesheet>
    

    输出

    <html>
       <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
          <title>bombers FC</title>
       </head>
       <body>
          <p>NUMBER OF INTERNATIONAL PLAYERS IS: 2</p>
          <p>AVERAGE HEIGHT OF INTERNATIONAL PLAYERS IS: 5.375</p>
       </body>
    </html>
    

    【讨论】:

    • 还修正了“意图”而不是“国际”错字。
    • 你能帮我看看国际球员的平均身高吗?请
    • @troy - 我进行了编辑以显示国际球员的平均身高。也许值得一票? ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2017-10-07
    相关资源
    最近更新 更多