XSLT显示优点徽章的麻烦

时间:2014-11-11 18:09:35

标签: xml xslt

我在试图显示1个侦察兵所有的优点徽章时遇到了麻烦......我无法找到一个有效的解决方案。例如,我希望它显示如下(带换行符):

First Aid
Swimming
Life saving
...

到目前为止,它只显示每个侦察兵的第一个,即急救

这是XML:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE bsa SYSTEM "assign08.dtd">
<?xml-stylesheet type="text/xsl" href="assign08.xsl"?>
<bsa>
  <council name = "jedi">
    <troop number = "01" unitname= "ok">
  <scout>
    <firstname>John</firstname>
    <lastname>Smith</lastname>
    <phone>111-111-1111</phone>
    <address>
      <street>Fork St.</street>
      <city>Portland</city>
      <state>OR</state>
      </address>
    <rank date-earned = "1/1/01">Eagle</rank>
    <meritbadge date-earned = "1/1/00">First Aid</meritbadge>
    <meritbadge date-earned = "2/1/00">Swimming</meritbadge>
    <meritbadge date-earned = "3/1/00">Life Saving</meritbadge>
    <meritbadge date-earned = "4/1/00">Environmental Science</meritbadge>
    <meritbadge date-earned = "5/1/00">Camping</meritbadge>
    <meritbadge date-earned = "6/1/00">Cooking</meritbadge>
    <meritbadge date-earned = "7/1/00">Safety</meritbadge>
    <meritbadge date-earned = "8/1/00">Basket Weaving</meritbadge>
    <meritbadge date-earned = "9/1/00">Wood carving</meritbadge>
    <meritbadge date-earned = "10/1/00">Archery</meritbadge>
    </scout>
  <scout>
    <firstname>Bill</firstname>
    <lastname>Joel</lastname>
    <phone>111-111-1112</phone>
    <address>
      <street>Fork St.</street>
      <city>Portland</city>
      <state>OR</state>
      </address>
    <rank date-earned = "2/2/02">Eagle</rank>
    <meritbadge date-earned = "1/1/00">First Aid</meritbadge>
    <meritbadge date-earned = "2/1/00">Swimming</meritbadge>
    <meritbadge date-earned = "3/1/00">Life Saving</meritbadge>
    <meritbadge date-earned = "4/1/00">Environmental Science</meritbadge>
    <meritbadge date-earned = "5/1/00">Camping</meritbadge>
    <meritbadge date-earned = "6/1/00">Cooking</meritbadge>
    <meritbadge date-earned = "7/1/00">Safety</meritbadge>
    <meritbadge date-earned = "8/1/00">Basket Weaving</meritbadge>
    <meritbadge date-earned = "9/1/00">Wood carving</meritbadge>
    <meritbadge date-earned = "10/1/00">Archery</meritbadge>
    </scout>

...

这是XSL:

<?xml version="1.0" encoding="UTF-8"?>

 <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
<html>
<body>
<h2>Buy Scouts of America</h2>
<table border="1">
 <tr bgcolor="#9acd32">
   <th>Last Name</th>
     <th>First Name</th>
   <th>Phone Number</th>
     <th>Address</th>
     <th>Rank</th>
     <th>Merit Badges</th>
 </tr>
 <xsl:for-each select="bsa/council/troop/scout">
  <xsl:sort select="lastname"/>
 <tr>
   <td><xsl:value-of select="lastname"/></td>
     <td><xsl:value-of select="firstname"/></td>
   <td><xsl:value-of select="phone"/></td>
     <td><xsl:value-of select="address"/></td>
     <td><xsl:value-of select="rank"/></td>
     <td><xsl:value-of select="meritbadge"/></td>
 </tr>
 </xsl:for-each>
  </table>
  </body>
 </html>
</xsl:template>

</xsl:stylesheet> 

1 个答案:

答案 0 :(得分:1)

这是因为您正在使用XSLT 1.0和xsl:value-of。在1.0中,xsl:value-of只会为您提供第一次出现的值。

请尝试使用<xsl:apply-templates select="meritbadge"/>。如果您需要更改输出内容的格式,请添加与meritbadge匹配的模板,然后根据需要添加格式。例如:

<xsl:template match="meritbadge">
    <xsl:value-of select="."/>
    <br/>
</xsl:template>

另外(无关)你有&#34;购买美国童子军&#34;而不是&#34; 男孩美国童子军&#34;。

编辑 - 修改过的XSLT的完整示例

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <body>
                <h2>Buy Scouts of America</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Last Name</th>
                        <th>First Name</th>
                        <th>Phone Number</th>
                        <th>Address</th>
                        <th>Rank</th>
                        <th>Merit Badges</th>
                    </tr>
                    <xsl:for-each select="bsa/council/troop/scout">
                        <xsl:sort select="lastname"/>
                        <tr>
                            <td><xsl:value-of select="lastname"/></td>
                            <td><xsl:value-of select="firstname"/></td>
                            <td><xsl:value-of select="phone"/></td>
                            <td><xsl:value-of select="address"/></td>
                            <td><xsl:value-of select="rank"/></td>
                            <td><xsl:apply-templates select="meritbadge"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="meritbadge">
        <xsl:value-of select="."/>
        <br/>
    </xsl:template>

</xsl:stylesheet>

编辑#2 - 独立的议会/部队

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <html>
            <body>
                <h1>Boy Scouts of America</h1>
                <xsl:apply-templates select="council"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="council">
        <h2><xsl:value-of select="@name"/></h2>
        <xsl:apply-templates select="troop"/>
    </xsl:template>

    <xsl:template match="troop">
        <h3><xsl:value-of select="concat(@number,' - ',@unitname)"/></h3>
        <table border="1">
            <tr bgcolor="#9acd32">
                <th>Last Name</th>
                <th>First Name</th>
                <th>Phone Number</th>
                <th>Address</th>
                <th>Rank</th>
                <th>Merit Badges</th>
            </tr>
            <xsl:apply-templates select="scout">
                <xsl:sort select="lastname"/>
            </xsl:apply-templates>
        </table>
    </xsl:template>

    <xsl:template match="scout">
        <tr>
            <td><xsl:apply-templates select="lastname"/></td>
            <td><xsl:apply-templates select="firstname"/></td>
            <td><xsl:apply-templates select="phone"/></td>
            <td><xsl:apply-templates select="address"/></td>
            <td><xsl:apply-templates select="rank"/></td>
            <td><xsl:apply-templates select="meritbadge"/></td>
        </tr>
    </xsl:template>

    <xsl:template match="meritbadge|address/*">
        <xsl:value-of select="."/>
        <br/>
    </xsl:template>

</xsl:stylesheet>