usamazf
usamazf

Reputation: 3215

Is it possible to call a single XSLT Template for two or more nodes having different XPATH?

I am kind of not sure about this if its possible or not but I really do hope it is. So what I want to do goes something like this:

I have an XML structured as follows:

<RootNode>
    <Node1>
        <DataSet1> <!--Some XML Tags here not necessary--> </DataSet1>
        <DataSet2> <!--Some XML Tags here not necessary--> </DataSet2>
        <DataSet3> <!--Some XML Tags here not necessary--> </DataSet3>
    </Node1>

    <Node2>
        <DataSet1> <!--Some XML Tags here not necessary--> </DataSet1>
        <DataSet2> <!--Some XML Tags here not necessary--> </DataSet2>
        <DataSet3> <!--Some XML Tags here not necessary--> </DataSet3>
    </Node2>

    <Node3>
        <DataSet1> <!--Some XML Tags here not necessary--> </DataSet1>
        <DataSet2> <!--Some XML Tags here not necessary--> </DataSet2>
        <DataSet3> <!--Some XML Tags here not necessary--> </DataSet3>
    </Node3>        
</RootNode>

What I am trying to do is to display the datasets in HTML Tables using XSLT... I want to know if it is possible to implement a single template for the similar dataset elements. Like Node1/DataSet1, Node2/DataSet1 and Node3/DataSet1 would be implemented via single template instead of triplicating the code for each possible XPATH.

Please note that the this is a sample XML and names of parent nodes i.e. Node1, Node2, Node3 etc are unique and can't be modified to make them same.

Upvotes: 0

Views: 51

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

If all the element names are known in advance, you can list them in the match pattern, e.g:

<xsl:template match="DataSet1 | DataSet2 | DataSet3"> 

If the element names are numbered as shown in your example, you could use:

<xsl:template match="*[starts-with(name(), 'DataSet')]">

If the element names are completely unknown, you could use a wild card, e.g:

<xsl:template match="*">

To restrict the scope of such template, you could also specify the level, e.g.:

<xsl:template match="/RootNode/*/*">

or use a mode.

Upvotes: 2

Related Questions