Conditionally wrap content with XSL 1.0

喜夏-厌秋 提交于 2019-12-06 07:35:15

This should do it:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="text()" />

  <xsl:template match="root[attributionUrl !='' and attribution != '']">
    <a href="{attributionUrl}">
      <xsl:apply-templates select="attribution" />
    </a>
  </xsl:template>

  <xsl:template match="attribution[. != '']">
    <span>
      Thank you, <xsl:value-of select="attribution"/>
    </span>
    <div>Lots of content ...</div>
  </xsl:template>
</xsl:stylesheet>

You can have a template that does the "Thank you" stuff and use it for both cases. Using template matching instead of <xsl:if> or <xsl:when> is more in the spirit of XML anyway:

<xsl:template match="root[attributionUrl!='']">
  <a href="{attributionUrl}">
    <xsl:call-template name="thankYou"/>
  </a>
</xsl:template>

<xsl:template match="root" name="thankYou">
  <span>Thank you, <xsl:value-of select="attribution"/></span>
  <div>Lots of content ...</div>
</xsl:template>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!