开发者

Reuse xslt template to format xml elements

I have an xml file

<catalog>
  <s1>
    <cd>
      <title>TRACK A</title>
      <artist>ARTIST A</artist>
    </cd>
  </s1>
  <s2>
    <cd>
      <title>TRACK B</title>
      <artist>TRACK B</artist>
    </cd>
  </s2>
  <s3>
    <cd>
      <title>TRACK C</title>
      <artist>ARTIST C</artist>
    </cd>
    <cd>
      <title>TRACK D</title>
      <artist>ARTIST D</artist>
    </cd>
  </s3>
</catalog>

I am trying to set up templates to format elements of s1 and s3 the sam开发者_Python百科e, but format elements of s2 differently.

The xslt I have is

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

<xsl:template match="/">

  <xsl:for-each select="catalog/s1">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

  <xsl:for-each select="catalog/s2">
    <xsl:call-template name="style2"/>    
  </xsl:for-each>

  <xsl:for-each select="catalog/s3">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

</xsl:template>

<xsl:template match="cd" name="style1">
  <b><xsl:value-of select="title" /></b>
  <b><xsl:value-of select="artist" /></b>
</xsl:template>

<xsl:template match="cd" name="style2">
  <i><xsl:value-of select="title" /></i>
</xsl:template>

</xsl:stylesheet>

But it isn't producing any output. I think I need but doing so seems to call 'style 1' regardless.

Why isn't this producing output?

Thank you

Ryan


Put "cd/" in your select statements:

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

<xsl:template match="/">

  <xsl:for-each select="catalog/s1">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

  <xsl:for-each select="catalog/s2">
    <xsl:call-template name="style2"/>    
  </xsl:for-each>

  <xsl:for-each select="catalog/s3">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

</xsl:template>

<xsl:template match="cd" name="style1">
  <b><xsl:value-of select="cd/title" /></b>
  <b><xsl:value-of select="cd/artist" /></b>
</xsl:template>

<xsl:template match="cd" name="style2">
  <i><xsl:value-of select="cd/title" /></i>
</xsl:template>

</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜