已设置属性的Concat param

时间:2013-07-14 00:09:41

标签: xslt

在XSLT 1.0中是否可以根据实际xslt文档中“先前输出”的另一个值输出值?

我似乎找不到正确的方式来说这个。希望这个例子应该易于理解。

<xsl:stylesheet>
  <xsl:param name="ServerUrl" select="'http://www.myserver.com/'"/>
  <xsl:template match="/">
    <html>
      <body>
        <img src="images/image1.jpg">
          <xsl:attribute name="src">
            <xsl:value-of select="concat($ServerUrl,**Value of current @src**)" />
          </xsl:attribute>
        </img>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我想要以下输出:

<html>
  <body>
    <img src="http://www.myserver.com/images/image1.jpg"></img>
  </body>
</html>

我知道这一开始可能看起来不对,但目的是让XSLT尽可能地与原始HTML保持一致,以便进一步修改。

2 个答案:

答案 0 :(得分:2)

您希望在文字结果元素的属性中使用XPath标记的结果。

在XSLT中,“属性值模板”(AVT)用于此目的。要使用AVT,您应该使用打开和关闭花括号来包围XPath表达式。 AVT可以与同一属性中的文字文本组合,从而节省了使用concat表达式的需要。

因此,对于您的示例,您可以使用:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform>
  <xsl:param name="ServerUrl" select="'http://www.myserver.com/'"/>
  <xsl:template match="/">
    <html>
      <body>
        <img src="{$ServerUrl}images/image1.jpg"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:1)

以下样式表使用带有空路径的document()函数,它将XSLT作为XML文档加载,然后将XPath加载到img/@src属性值:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">
    <xsl:param name="ServerUrl" select="'http://www.myserver.com/'"/>
    <xsl:template match="/">
        <html>
            <body>
                <img src="images/image1.jpg">
                    <xsl:attribute name="src">
                        <xsl:value-of select="concat($ServerUrl, document('')/xsl:stylesheet/xsl:template[@match='/']/html/body/img/@src)" />
                    </xsl:attribute>
                </img>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

第二个@src属性定义将“获胜”并在输出中生成。

7.1.3 Creating Attributes

  

向元素添加属性会替换任何现有属性   该元素具有相同的扩展名。

虽然,我不推荐这种方法。阅读/理解而不是标准做法令人困惑。

相关问题