开发者

How to declare variable as a link in XSLT

Hi is there away on how to declare a link(ie:http://www.google.com) as a variable and then using the variable for an else if?Something like this?

   <xsl:element name="a">开发者_开发百科; 
<xsl:attribute name="href">http://www.google.com</xsl:attribute>// first get the link
<xsl:choose>
<xsl:when test="http://www.google.com">
  Do something 1
</xsl:when>
<xsl:otherwise>
  Do something 2
</xsl:choose> 
</xsl:element>

Is this possible?What should i be looking at?


is there away on how to declare a link(ie:http://www.google.com) as a variable and then using the variable for an else if?

Use this code as a working example -- of course you need to learn at least the basics of XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vLink" select="'http://www.google.com'"/>

 <xsl:template match="/">
  <xsl:choose>
    <xsl:when test="$vLink = 'http://www.google.com'">
      It is the Google link...
    </xsl:when>
    <xsl:otherwise>
     It is not (exactly) the Google link...
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on any XML document (not used), the wanted result is produced:

  It is the Google link...

One can also use a global <xsl:param>. This can be set externally by the invoker of the transformation.


Match against the content straight forward, and declare the URL as a variable.


If you need it more globally try this:

...
<xsl:apply-templates select="a" />
... 

<xsl:template match="a">
    Just a link
</xsl:template>

<xsl:template match="a[starts-with(@href, 'http://google.com/') or starts-with(@href, 'http://www.google.com/')]">
    Link to google.com
</xsl:template> 


It's possible to some extent, but there is no if-else construct in XSL. Here's a version I tested that you might be able to adapt to your needs. The input I used was:

<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<xml>
    <LinkValue>http://www.google.com/</LinkValue>
</xml>

The XSL that showing "Do something 1" if LinkValue was the string above or "Do something 2" if I modified it was:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="LinkValue" select="//LinkValue"/>
        <xsl:element name="a">  
            <xsl:attribute name="href"><xsl:value-of select="$LinkValue"/></xsl:attribute>
            <xsl:if test="$LinkValue = 'http://www.google.com/'"> 
                Do something 1 
            </xsl:if> 
            <xsl:if test="$LinkValue != 'http://www.google.com/'"> 
                Do something 2 
            </xsl:if>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Hopefully you can use these samples to figure out exactly what you need to implement for your scenario.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜