Conditionally wrap content with XSL 1.0

2019-05-27 01:49发布

I am looking for a way to wrap content with xsl. This is a simplified example of what I am doing. Lot's of content ... is a significant amount of content, and the anchor tag is only used as an example. It could be a div or anything else.

XML:

<root>
    <attribution>John Smith</attribution>
    <attributionUrl>http://www.johnsmith.com</attributionUrl>
</root>

XSL: How I am currently doing it. This is adding a significant amount of xsl and I am sure there is a way to simplify.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <xsl:if test="attribution != ''">
        <xsl:choose>
            <xsl:when test="attributionUrl != ''">
                <a>
                    <xsl:attribute name="href"><xsl:value-of select="attributionUrl"/></xsl:attribute>
                    <span>Thank you, <xsl:value-of select="attribution"/></span>
                    <div>Lots of content ...</div>
                </a>
            </xsl:when>
            <xsl:otherwise>
                <span>Thank you, <xsl:value-of select="attribution"/></span>
                    <div>Lots of content ...</div>
            </xsl:otherwise>
        </xsl:choose>   
    </xsl:if>
</xsl:template>

XSL: Conceptually this is what I want to do. It doesn't work because it is invalid XML, but it does capture the idea.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <xsl:if test="attributionUrl != ''">
        <a>
    </xsl:if>

    <xsl:attribute name="href"><xsl:value-of select="attributionUrl"/></xsl:attribute>
    <span>Thank you, <xsl:value-of select="attribution"/></span>
    <div>Lots of content ...</div>

    <xsl:if test="attributionUrl != ''">
        </a>
    </xsl:if>
</xsl:template>

EDIT:

I am trying to avoid the multiple versions of <div>Lots of content ...</div>

2条回答
对你真心纯属浪费
2楼-- · 2019-05-27 02:20

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>
查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-27 02:25

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>
查看更多
登录 后发表回答