views:

76

answers:

2

Hi I have an XML as below

<result>  
   <binding name="PropertyURI">
      <uri>http://dbpedia.org/ontology/motto&lt;/uri&gt;
   </binding> 
   <binding name="Property">
      <literal xml:lang="en">motto</literal>
   </binding>
   <binding name="ValueURI">
      <uri>http://dbpedia.org/ontology/motto&lt;/uri&gt;
   </binding>
   <binding name="Value">
      <literal>Ittehad, Tanzim, Yaqeen-e-Muhkam(Urdu)</literal>
   </binding>
</result>

I want to transform it like

<a href=PropertyURI>Property</a>
<a href=ValueURI>Value</a>

Problem is that number of binding tags can different. Sometimes we may have only URIs or ony Values. How can I know in XSLT that if binding with @name=PropertyURI is available. If yes then what is the name of next binding @name attribute?

A: 

It's not quite clear what you're looking for, since your description of the problem and your desired output don't seem to be related. I think that what you want to do is find every binding element with a uri child, find the related binding element that has a literal child, and use the uri and literal values to populate an a element.

This template assumes that the two binding elements are related because the name attribute on the one with the uri child starts with the name attribute on the one with the literal child, e.g. "PropertyURI" starts with "Property" and "ValueURI" starts with "Value":

<xsl:template match="binding[uri]">
   <xsl:variable name="name" value="@name"/>
   <xsl:variable name="literal" 
                select="/result/binding[starts-with($name, @name)]/literal"/>
   <xsl:if test="$literal">
      <a href="{uri}">
         <xsl:value-of select="$literal"/>
      </a>
   </xsl:if>
</xsl:template>

If the related element is simply the next binding element after the one with the uri child, use the above template, replacing the variable assignment with:

<xsl:variable name="literal" select="following-sibling::binding[1]/literal"/>
Robert Rossney
I am getting an error. Attribute value is not allowed on this element <xsl:variable name="name" value="@name"/>
Taz
Sorry just some problem with editor. There is no error after I rewrote the same thing.
Taz
+1  A: 

There is already an answer that seems valid but I've just spent 10 minutes testing the following code so :

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<xsl:template match="/">
  <xsl:apply-templates select="/result/binding[@name='PropertyURI']"/>
</xsl:template>
  <xsl:template match="binding">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="./uri"/>
      </xsl:attribute>
      <xsl:value-of select="./following-sibling::binding[1][@name='Property']/literal"/>
    </a>
  </xsl:template>
</xsl:stylesheet> 
FenchKiss Dev
Thanks for the replies. I have tested the one by FrenchKiss. Yes this is fine and I will get a correct link. 1. what if there wasnt a URI but Just a Literal? In this case what I want Property URI to serve both as href and text in anchor/link. and If there is only Property but no PropertyURI then it will be converted to simple text instead of a link. 2. What about ValueURI and Value?
Taz