user3804306
user3804306

Reputation: 3

Merging XSLTs into one flle and calling each template separately matching specific content

I've the below Sample XMLs.

XML1

<root num="1">
 <abc></abc>
 <cde></cde>
 <def></def>
</root>

XML2

<root num="2">
 <xyz></xyz>
 <cft></cft>
 <vft></vft>
</root>

XML3

<root num="3">
 <dfg></dfg>
 <mnb></mnb>
 <gft></gft>
<root>

And i have 3 different XSLTs, each corresponding to XML.

I want to achieve the below. Make a single XSLT and call the template based on the root number. something like the below.

<xsl:if test="root[@num="1"]>
<!--Call the template matching root 1-->
</xsl:if>
<xsl:if test="root[@num="2"]>
<!--Call the template matching root 2-->
</xsl:if>
<xsl:if test="root[@num="3"]>
<!--Call the template matching root 3-->
</xsl:if>

I just want to put all the XSLTs in a single XSLT, please let me know how can i do this.

Thanks

Upvotes: 0

Views: 41

Answers (1)

dmportella
dmportella

Reputation: 4724

You can use element.

The element contains rules to apply when a specified node is matched.

The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).

Note:  is a top-level element.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="cd">
  <p>
  <xsl:apply-templates select="title"/>
  <xsl:apply-templates select="artist"/>
  </p>
</xsl:template>

<xsl:template match="title">
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

<xsl:template match="artist">
  Artist: <span style="color:#00ff00">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

</xsl:stylesheet> 

This was taken from http://www.w3schools.com/xsl/el_template.asp

Upvotes: 1

Related Questions