1

对于精通 XSLT 的作者来说,这个问题可能很容易回答。

我有以下示例 XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student num="12345678">
        <name>Dona Diller</name>
        <dob>1970-07-21</dob>
        <education>BSc</education>
        <education>MSc</education>
        <status>Married</status>
    </student>

<!-- more student elements to follow... -->

</students>

以及以下 XSL:

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

    <xsl:template match="/">
        <html>
            <title>Test</title>
            <body>
                <h1>Personal details</h1>
                <xsl:apply-templates select="students/student"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="student">
        <p>
            Student number:
            <xsl:value-of select="@num"/>
        </p>
        <p>Full name:
            <xsl:value-of select="name"/>
        </p>
        <p>Date of birth
            <xsl:value-of select="dob"/>
        </p>
        <!-- TODO: text of 'education' elements must be separated by a space -->
        <p>Degrees:
            <xsl:apply-templates select="education"/>
        </p>
        <p>Status:
            <xsl:value-of select="status"/>
        </p>
    </xsl:template>

</xsl:stylesheet>

当应用于本文开头包含的 XML 文档时,它会产生以下 XHTML 输出:

<html>
   <title>Test</title>
   <body>
      <h1>Personal details</h1>
      <p>
         Student number:
         12345678
      </p>
      <p>Full name:
         Dona Diller
      </p>
      <p>Date of birth
         1970-07-21
      </p>
      <p>Degrees:
         BScMSc
      </p>
      <p>Status:
         Married
      </p>
   </body>
</html>

我的问题是学位名称合并为一个字符串(教育元素的文本)。因此,我不想在输出中获得“BScMSc”,而是想在前面的示例中显示“BSc MSc”。有什么想法吗?

4

4 回答 4

4

The solutions by Novatchev and Honnen will both work (of course) but something about them feels a little unsatisfactory to me. I think if you're going to delegate formatting of an education element to a template rule, then that template rule ought only to be concerned with the formatting of one education element, and not with the formatting of a set of adjacent elements. To me, the interstitial spacing is properly the job of the parent template. So I think I would be inclined to write:

<xsl:for-each select="education">
  <xsl:if test="position() ne 1"><xsl:text> </xsl:text></xsl:if>
  <xsl:apply-templates select="."/>
</xsl:for-each>

But it's a matter of opinion.

于 2012-04-29T20:36:44.773 回答
2

添加模板

<xsl:template match="education">
  <xsl:if test="position() > 1">
    <xsl:text> </xsl:text>
  </xsl:if>
  <xsl:value-of select="."/>
</xsl:template>
于 2012-04-29T16:38:08.830 回答
1

更短/更简单/更易读

添加此模板

<xsl:template match="education[position() > 1]">
  <xsl:value-of select="concat(' ', .)"/>
</xsl:template>
于 2012-04-29T16:59:34.480 回答
0

只需在每个学位的末尾添加一个空格:

<xsl:template match="education">
    <xsl:value-of select="concat(.,' ')"/>
</xsl:template>

是的,这会在度数列表的末尾产生一个多余的空间,但在这种情况下这不是问题。

如果你有 XSLT 2.0,你可以试试

<p>Degrees:
    <xsl:value-of select="string-join(education,' ')"/>
</p>
于 2012-04-30T03:55:53.693 回答