views:

58

answers:

2

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 value, 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)"/&gt;
</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"

+3  A: 

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.

Welbog
but the resource attribute is not just having the ISBN value only, it could be an URL or other things. Furthermore, the resource attribute is not necessary being the first attribute that appear inside an element.
louis
+1  A: 

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

Macmade
`replace()` doesn't exist in XSLT 1.0, but if louis is using XSLT 2.0, then that is definitely a good option.
Welbog