开发者

Need to convert an ISBN value into an Amazon search query link using XSLT

I am using XSL to transform a resource attribute, it will check whether or not it contains "urn:ISBN". If it does, then it will need to get the ISBN number valu开发者_运维知识库e, and return it as an Amazon search query. For example

resource="urn:ISBN:0752820907"

then change into

http://www.amazon.com/gp/search/ref=sr_adv_b/?field-isbn=0752820907

The problem is, I don't know how to perform string matching, and also string copying using XSL. Please help, thanks.

update

below is my XSL code, it still couldn't produce the result I want. The hyperlink generated is intended to be placed on a column of a table

<td style="background-color: #F7F6F3">
<xsl:choose>

<xsl:when test="*/@rdf:resource">

<xsl:if test="starts-with(text(),'urn:ISBN')">
<xsl:variable name="ISBN" select="substring-after(text(),'urn:ISBN:')"/>
<xsl:value-of 
select="concat ('http://www.amazon.com/gp/search/ref=sr_adv_b/?field-isbn=',$ISBN)"/>
</xsl:if>
</xsl:when>
</xsl:choose>
</td>

the data I am working on is RDF in XML format.

the usual representation involving books is rel="foaf:interest" resource="urn:ISBN:0752820907"


Unfortunately, XSLT is pretty lacking in string manipulation functionality. Fortunately, the kind of thing that you're trying to do isn't really that difficult.

You need to start by looking at the starts-with() XPath function. It returns true when the first parameter starts with the second parameter.

So you can build an xsl:if element with a starts-with() in the test, to see if the string starts with "urn:ISBN":

<xsl:if test="starts-with(text(),'urn:ISBN')">
   <!-- This will be executed if the current node starts with 'urn:ISBN' -->
</xsl:if>

The next thing you need to do is build the URL. That's easy, too, using the concat() and substring-after() functions:

<xsl:if test="starts-with(text(),'urn:ISBN')">
  <xsl:variable name="ISBN" select="substring-after(text(),'urn:ISBN:')"/>
  <xsl:value-of 
    select="concat('http://www.amazon.com/gp/search/ref=sr_adv_b/?field-isbn=',$ISBN)"
  />
</xsl:if>

What that snippet is doing is parsing out the ISBN (which is whatever comes after "urn:ISBN") and stores it in a variable. It then concatenates that variable with the Amazon URL to produce the final result.


You can use the starts-with( str1, str2 ) XSL function to test your string, and then the replace( str, pattern, replace ) one.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜