Could someone help me out with an unfinished idea?
Im not very used to xslt but im trying and thanks to a lot of internet help im getting along.
But:
following scenario i cannot solve:
Given an example XML with some geological layer data looking like that:
<LAYERS>
<LAYER DEPTHTO="1.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="94.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="94.20" INTV="1" INDEX_ZONE="-1" EGART="Lost_Data"/>
<LAYER DEPTHTO="95.00" PETRO="Gravel" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="100.00" PETRO="Sand" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="100.50" PETRO="Mud" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="101.50" PETRO="Sand" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="101.80" PETRO="Mud" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="102.90" PETRO="Mud" STRAT="geologiscal_formation_3" INTV="1"/>
<LAYER DEPTHTO="103.00" PETRO="Sand" STRAT="geologiscal_formation_3" INTV="1"/>
<LAYER DEPTHTO="103.25" INTV="1" INDEX_ZONE="-1" EGART="Lost_Data"/>
<LAYER DEPTHTO="103.69" PETRO="Sand" STRAT="geologiscal_formation_3" INTV="1"/>
<LAYER DEPTHTO="104.00" PETRO="Mud" STRAT="geologiscal_formation_3" INTV="1"/>
</LAYERS>
and using following xslt code:
<?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:key name="adj" match="LAYER" use="generate-id(preceding-sibling::LAYER[not(@STRAT = current()/@STRAT)][1])" />
<xsl:template match="/*">
<xsl:for-each select="LAYER[generate-id() = generate-id(key('adj', generate-id(preceding-sibling::LAYER[not(@STRAT = current()/@STRAT)][1]))[1])]">
<xsl:variable name="INTVID" select="@INTV"/>
<xsl:variable name="precedingZone" select="preceding-sibling::LAYER[@INTV = $INTVID][1]"/>
<xsl:variable name="DEPTHFROM" select="$precedingZone/@DEPTHTO"/>
<xsl:if test="@STRAT != ''">
<xsl:variable name="current-group" select="key('adj', generate-id(preceding-sibling::LAYER[not(@STRAT = current()/@STRAT)][1]))" />
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT"/>
<xsl:text>" </xsl:text>
<xsl:for-each select="$current-group">
<xsl:sort select="$DEPTHFROM" data-type="number" order="ascending"/>
<xsl:if test="position() = 1 ">
<xsl:value-of select="$DEPTHFROM"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:for-each select="$current-group">
<xsl:sort select="@DEPTHTO" data-type="number" order="descending"/>
<xsl:if test="position() = 1 ">
<xsl:value-of select="@DEPTHTO"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:value-of select="@INDEX_ZONE"/>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
the result looks like that:
ZONE "geologiscal_formation_1" 94.00
ZONE "geologiscal_formation_1" 94.20 95.00
ZONE "geologiscal_formation_2" 95.00 101.80
ZONE "geologiscal_formation_3" 101.80 103.00
ZONE "geologiscal_formation_3" 103.25 104.00
But the desired output should look like that:
ZONE "geologiscal_formation_1" 0.00 95.00
ZONE "geologiscal_formation_2" 95.00 101.80
ZONE "geologiscal_formation_3" 101.80 104.00
More background information: Since the first line gives a DEPTHTO data only, i assume it starts at 0, as long not known. The grouping does work partly only, because we got some layers with "lost_data", which brings all the idea to a bad result.
So i need some kind of condition, that ignores/skips data where "lost_data" is found. But since im using "for-each" i dont know how to accomplish that. I heard of some "pipeline processing" but havent looked much into it, since im already overwhelmed with what i got.
Maybe someone knows better?
And ye, im stuck on xslt 1.0 :)
Thanks for any ideas or help.
Thanks to the answer and with some minor changes i was able to achive my goal:
the changes look like that:
<?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:key name="layer-by-strat" match="LAYER" use="@STRAT" />
<xsl:template match="/*">
<xsl:for-each select="LAYER[not(@EGART='Lost_Data')][count(. | key('layer-by-strat', @STRAT)[1]) = 1]">
<xsl:variable name="INTVID" select="@INTV"/>
<xsl:variable name="precedingZone" select="preceding-sibling::LAYER[@INTV = $INTVID][1]"/>
<xsl:variable name="DEPTHFROM" select="$precedingZone/@DEPTHTO"/>
<xsl:choose>
<xsl:when test="$precedingZone">
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT" />
<xsl:text>" </xsl:text>
<xsl:value-of select="$DEPTHFROM"/>
<xsl:text> </xsl:text>
<xsl:value-of select="key('layer-by-strat', @STRAT)[last()]/@DEPTHTO" />
<xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT" />
<xsl:text>" 0.00 </xsl:text>
<xsl:value-of select="key('layer-by-strat', @STRAT)[last()]/@DEPTHTO" />
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
CodePudding user response:
The result you show can be produced quite easily using:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:key name="layer-by-strat" match="LAYER" use="@STRAT" />
<xsl:template match="LAYERS" >
<xsl:for-each select="LAYER[not(@EGART='Lost_Data')][count(. | key('layer-by-strat', @STRAT)[1]) = 1]">
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT" />
<xsl:text>" 0.00 </xsl:text>
<xsl:value-of select="key('layer-by-strat', @STRAT)[last()]/@DEPTHTO" />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This is assuming the last LAYER
in each group has the wanted maximum value - otherwise a minor adjustment will be required.
Added:
I notice I have missed a part of the requirement where each layer begins at the maximal depth of the previous one. Here is the adjusted stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:key name="layer-by-strat" match="LAYER" use="@STRAT" />
<xsl:template match="LAYERS" >
<xsl:call-template name="generate-rows">
<xsl:with-param name="layers" select="LAYER[not(@EGART='Lost_Data')][count(. | key('layer-by-strat', @STRAT)[1]) = 1]"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="generate-rows">
<xsl:param name="layers" select="/.."/>
<xsl:param name="accumulated-depth" select="'0.00'"/>
<xsl:if test="$layers">
<xsl:variable name="strat" select="$layers[1]/@STRAT" />
<xsl:variable name="max-depth" select="key('layer-by-strat', $strat)[last()]/@DEPTHTO" />
<!-- output -->
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="$strat" />
<xsl:text>" </xsl:text>
<xsl:value-of select="$accumulated-depth" />
<xsl:text> </xsl:text>
<xsl:value-of select="$max-depth" />
<xsl:text> </xsl:text>
<!-- recursive call -->
<xsl:call-template name="generate-rows">
<xsl:with-param name="layers" select="$layers[position() > 1]"/>
<xsl:with-param name="accumulated-depth" select="$max-depth"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>