XSLT:如果存在值,请添加逗号

时间:2011-11-25 23:53:08

标签: xml xslt

我是XML和XSLT的新手。我有一个家庭作业,要求我们根据一些XML数据生成一个页面。这很简单,所以在这里:

我的XML数据如下所示:

<convention>
    <title>ABC Web Development Conference</title>
    <location>ABC College, San Diego, CA</location>
    <date> March 25, 2011 - March 27, 2011</date>
    <exhibitor>
        <name>John Smith</name>
        <address1>1234 2nd Avenue</address1>
        <address2></address2>
        <city>San Diego</city>
        <state>CA</state>
        <email>john@john.com</email>
        <specialization>
            <subject>Web Development</subject>
            <subject>Software Design</subject>
        </specialization>
    </exhibitor>
        <exhibitor>
        <name>Jane Smith</name>
        <address1>1234 First Avenue</address1>
        <address2>Ste 123</address2>
        <city>San Diego</city>
        <state>CA</state>
        <email>jane@jane.com</email>
        <specialization>
            <subject>Web Development</subject>
        </specialization>
    </exhibitor>
</convention>

基本上,我已将其设置为很好地输出到表中。当我输出地址2时,我的问题出现了。我想这样做,如果address2的字符串长度大于0,则输出一个逗号。

以下是我的XSL数据:

<table width="600" border="1" cellspacing="3" cellpadding="5">
  <tr>
    <td colspan="3" align="center"><xsl:value-of select="convention/title"/></td>
  </tr>
  <tr>
    <td colspan="3" align="center"><xsl:value-of select="convention/location"/></td>
  </tr>
  <tr>
    <td colspan="3" align="center"><xsl:value-of select="convention/date"/></td>
  </tr>
  <tr>
    <th>Exhibitor</th>
    <th>Address</th>
    <th>Specialization</th>
  </tr>
    <xsl:for-each select="convention/exhibitor">
    <tr>
      <td><xsl:value-of select="name"/><br /><xsl:value-of select="email"/></td>
      <td><xsl:value-of select="address1"/>, <xsl:value-of select="address2"/>&nbsp;<xsl:value-of select="city"/>, <xsl:value-of select="state"/></td>
      <td><xsl:for-each select="specialization/subject"><xsl:value-of select="."/><br /></xsl:for-each></td>
    </tr>
    </xsl:for-each>
  <tr>
    <td colspan="3">Total Number of Exhibitors: <xsl:value-of select="count(convention/exhibitor)"/></td>
  </tr>
</table>

非常感谢任何帮助:)

3 个答案:

答案 0 :(得分:3)

你有课本吗?我会给你一些提示,可以帮助你找到你需要的信息:

如果您不熟悉这些概念,我建议您从课程资料中查看更多信息。

祝你好运!

答案 1 :(得分:3)

与目前为止的答案相反, 无需使用显式XSLT条件指令

使用

<xsl:apply-templates select="address2"/>

并在XSLT样式表中添加此模板

<xsl:template match="address2[normalize-space()]">
  <xsl:value-of select="concat(', ', .)"/>
</xsl:template>

答案 2 :(得分:1)

使用xsl:if。你的作业很好看。再见。