使用 XSL 1.0 有条件地包装内容 [英] Conditionally wrap content with XSL 1.0

查看:33
本文介绍了使用 XSL 1.0 有条件地包装内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种用 xsl 包装内容的方法.这是我正在做的事情的一个简化示例.Lot's of content ...是大量内容,锚标签仅用作示例.它可以是一个 div 或其他任何东西.

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:我目前是如何做的.这是添加了大量的 xsl,我相信有一种方法可以简化.

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:从概念上讲,这就是我想要做的.它不起作用,因为它是无效的 XML,但它确实抓住了这个想法.

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>

我试图避免 <div>大量内容...</div>

推荐答案

应该这样做:

<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>

这篇关于使用 XSL 1.0 有条件地包装内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆