Home > Blockchain >  XSLT multiple templates in stylsheet with same match?
XSLT multiple templates in stylsheet with same match?

Time:05-07

Even there are almost the same question, i dont get the result dont for me.

The idea may be simple, but i dont understand all the processes in the background very well to solve that.

I got multiple templates with the same match showing different results.

A dataset extract from a follwoing node may look like that:

<LAYERS> 
    <LAYER DEPTHTO="93.63" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
    <LAYER DEPTHTO="94.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
    <LAYER DEPTHTO="95.00" PETRO="Gravel" STRAT="geologiscal_formation_1" INTV="1"/>
    <LAYER DEPTHTO="100.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
    <LAYER DEPTHTO="100.50" PETRO="Mud" STRAT="geologiscal_formation_1" INTV="1"/>
</LAYERS>  

and im trying to get a text output using multiple templates.

The wanted result should look like that using 3 templates:

Depth_to: 93.63
Depth_to: 94.00
Depth_to: 95.00
Depth_to: 100.00
Depth_to: 100.50
Petro:: Sand
Petro:: Sand
Petro:: Gravel
Petro:: Sand
Petro:: Mud
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1

And my idea in kinda-pseudo-xslt looks like that, trying to gather all templates in the top part (outcommented) while having the templates in blocks in the lower part:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xml="http://www.w3.org/XML/1998/namespace"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes=" xml xsl xs">
    <xsl:output method="text" version="1.0" indent="yes" />


<!--
  <xsl:template name="all_data" >
    <xsl:use-template name="path" />
    <xsl:use-template name="petro" />
    <xsl:use-template name="strat" />
  </xsl:template>
-->


  <xsl:template name="path" match="/" >
    <xsl:for-each select="LAYERS/LAYER">
      <xsl:text>Depth_to: </xsl:text>
      <xsl:value-of select="@DEPTHTO"/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>


  <xsl:template name="petro" match="/" >
    <xsl:for-each select="LAYERS/LAYER">
      <xsl:text>Petro:: </xsl:text>
      <xsl:value-of select="@PETRO"/>
      <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
  </xsl:template>


  <xsl:template name="strat" match="/" >
    <xsl:for-each select="LAYERS/LAYER">
      <xsl:text>Strat: </xsl:text>
      <xsl:value-of select="@STRAT"/>
      <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>

But that gives me only the result of one template:

Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1

I am sure there are other ways to get the result done, but the crucial question ist how to manage multiple templates for a result like that?

Thanks in advance for any kind of help :)

CodePudding user response:

This will do what you want.

<xsl:template match="LAYERS">
  <xsl:apply-templates select="LAYER/@DEPTHTO"/>
  <xsl:apply-templates select="LAYER/@PETRO"/>
  <xsl:apply-templates select="LAYER/@STRAT"/>
</xsl:template>

<xsl:template match="@DEPTHTO">
  <xsl:text>Depth_to: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template match="@PETRO">
  <xsl:text>Petro:: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template match="@STRAT">
  <xsl:text>Strat: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#xA;</xsl:text>
</xsl:template>

CodePudding user response:

If you do want to invoke multiple templates for the same node, the answer is to use modes:

<xsl:template match="/" mode="Depth">
 ...
</xsl:template>

<xsl:template match="/" mode="Petro">
 ...
</xsl:template>

<xsl:template match="/" mode="Strat">
 ...
</xsl:template>

...
<xsl:apply-templates select="." mode="Depth"/>
<xsl:apply-templates select="." mode="Petro"/>
<xsl:apply-templates select="." mode="Strat"/>

For your particular example this seems over-engineered, but perhaps your real task is more complicated.

  • Related