Xanthouse
Xanthouse

Reputation: 7

Create specific XSL with depending variables

I am trying to built a XSL(t) file to specify an importruleset for my application. My source XML is coming from SPARQL.

The problem is that the number of variables is different for every query. What I need is an XSL that takes the variables described in the head part of the xml and uses them as column headers. Then it must fill the rows with the data in the bindings for the corresponding headers.

So here is a part of my Source XML:

<?xml version='1.0' encoding='UTF-8'?>
<sparql xmlns='http://www.w3.org/2005/sparql-results#'>
    <head>
        <variable name='naam'/>
        <variable name='description'/>
        <variable name='uri'/>
    </head>
    <results>
        <result>
            <binding name='naam'>
                <literal>Zweepmast, thermisch verzinken</literal>
            </binding>
            <binding name='description'>
                <literal>Een zweepmast (uithoudercombinatie) dient thermisch te worden verzinkt volgens NEN-EN-ISO 1461 en vervolgens voorzien van een 2-laags natlak-of poedercoating, dit ter keuze van de fabrikant.</literal>
            </binding>
            <binding name='uri'>
                <uri>http://data.provincie-zuidholland.nl/id/62ac71da-b59e-ec11-b683-001dd8d70289</uri>
            </binding>
        </result>
        <result>
            <binding name='naam'>
                <literal>Verkeerskundige draagconstructie, afmetingen van grondstuk</literal>
            </binding>
            <binding name='description'>
                <literal>Een staander van een portaal of een gecombineerde lichtmast dient te worden voorzien van een grondstuk met een lengte van 2.000 mm. Het grondstuk is voorzien van minimaal twee grondvleugels met een minimaal formaat van 640 x 180 x 6 mm per grondvleugel. Het gebruik van demontabele grondvleugels of betonplaten is niet toegestaan.</literal>
            </binding>
            <binding name='uri'>
                <uri>http://data.provincie-zuidholland.nl/id/099f3e6f-b99e-ec11-b683-001dd8d70289</uri>
            </binding>
        </result>
    </results>
</sparql>
 

And this is in basic the ruleset within XSL I need to follow:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/results">
        <Import>
            <!-- This part is used to import all details for specifications that have a verification plan-->
            <xsl:for-each select="result/binding">
                <Row>
                                        
                    <!-- Name-->              
                    <xsl:attribute name="naam"><xsl:value-of select="current-group()[1]/literal/text()"/></xsl:attribute>
                    
                    <!-- Description -->
                    <xsl:attribute name="description"><xsl:value-of select="current-group()[1]/literal/text()"/></xsl:attribute>
                    
                    <!-- uri -->
                    <xsl:attribute name="uri"><xsl:value-of select="current-group()[1]/uri/text()" /></xsl:attribute>
                    
                </Row>
            </xsl:for-each>
        </Import>
    </xsl:template>
</xsl:stylesheet>

So the variables in this situation are 'naam', 'description' and 'uri'. For each <result>, the XSL need to create a row and fill in the "columns" with the <bindings>. Unfortunately, my application can not comprehend the current-group() function. So I am stuck here....

I tried to set up a XSL file with according to the ruleset. But I do not understand how an XSL can deal with variables.

Upvotes: 0

Views: 40

Answers (1)

y.arazim
y.arazim

Reputation: 3161

You could probably ignore the variables in the head and just create an attribute for each binding of a result, using its name attribute and the value of its child element.

And it seems you are limited to XSLT 1.0, so it would look like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.w3.org/2005/sparql-results#"
exclude-result-prefixes="ns0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/ns0:sparql">
    <Import>
        <xsl:for-each select="ns0:results/ns0:result">
            <Row>
                <xsl:for-each select="ns0:binding">
                    <xsl:attribute name="{@name}">
                        <xsl:value-of select="*"/>
                    </xsl:attribute>
                </xsl:for-each>
            </Row>
        </xsl:for-each>
    </Import>
</xsl:template>

</xsl:stylesheet>

Please note that the provided example XML declares a default namespace and that its root element is not results.

Upvotes: 1

Related Questions