If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Wednesday, June 13, 2012

XSLT not powerful, really??


<?xml version="1.0"?>
<sparql>
  <head>
    <variable name="subnode"/>
  </head>
  <results>
    <result>
      <binding name="subnode">
        <uri>http://data.kasabi.com/dataset/nxp-products/basicType/LPC1114FA44</uri>
      </binding>
    </result>
    <result>
      <binding name="subnode">
        <uri>http://data.kasabi.com/dataset/nxp-products/productTree/1498</uri>
      </binding>
    </result>
  </results>
</sparql>

We want to extract all identifiers for each URI on a newline saved as text. So expected output for this sample is
LPC1114FA44
1498

There are multiple ways to solve this but the solution below is a pretty easy one. It string-joins the last part of the tokenized uri with a newline character.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:output  method="text" encoding="UTF-8" media-type="text/plain"/>
  
  <xsl:template match="/">
    <xsl:variable name="uris" select="/sparql/results/result/binding/uri"/>
    <xsl:value-of select="string-join(for $uri in $uris return tokenize($uri, '/')[last()], '&#xa;')"/>
  </xsl:template>
  
</xsl:stylesheet>

No comments:

Post a Comment