Home > Software engineering >  XSLT Text output when specific Node exists only
XSLT Text output when specific Node exists only

Time:06-14

For a project im working on i created a stylesheet which works great.

A lot of help from the community, thanks!

Right now im trying to create a text output only when a specific node exists.

For examplme some soil layer data as xml:

<NODE1 Name="Well_1" Version="1 ">
  <LAYERS>
    <LAYER>
      <DEPTHTO>1.00</DEPTHTO>
      <INTV>0</INTV>
      <INDEX_ZONE>-1</INDEX_ZONE>
      <REC_CARB_MRKR>2</REC_CARB_MRKR>
    </LAYER>
    <LAYER>
      <DEPTHTO>2.00</DEPTHTO>
      <INTV>0</INTV>
      <INDEX_ZONE>-1</INDEX_ZONE>
    </LAYER>
    <LAYER>
      <DEPTHTO>3.00</DEPTHTO>
      <INTV>0</INTV>
      <INDEX_ZONE>-1</INDEX_ZONE>
      <REC_CARB_MRKR>2</REC_CARB_MRKR>
    </LAYER>
  </LAYERS>
</NODE1>

and with that xslt (MSXML 6; xslt1), dont bother some functionalities, its an extract:

<?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="REC" match="/*">
        <xsl:apply-templates select="." mode="WELLCURVE"/>
    </xsl:template>

    <xsl:template name="WELLCURVE" match="/*" mode="WELLCURVE">
        <xsl:text>SOME &#xA;</xsl:text>
        <xsl:text>TEXT &#xA;</xsl:text>
        <xsl:text>Options &#xA;</xsl:text>

        <xsl:for-each select="LAYERS/LAYER">
            <xsl:variable name="INTVID" select="INTV"/>
            <xsl:variable name="precedingMRKR" select="preceding-sibling::LAYER[INTV = $INTVID][1]"/>
            <xsl:variable name="DEPTHFROM" select="$precedingMRKR/DEPTHTO"/>
            <xsl:if test="INTV=0">
                <xsl:if test="REC_CARB_MRKR != ''">
                    <xsl:choose>
                        <xsl:when test="$precedingMRKR">
                            <xsl:variable name="Layer_mid" select="format-number(((DEPTHTO   $DEPTHFROM) div 2), '0.##')" />
                            <xsl:text>REC </xsl:text>
                            <xsl:value-of select="concat($Layer_mid,' ', REC_CARB_MRKR)" />
                            <xsl:text>&#xA;</xsl:text>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:variable name="Layer_mid" select="format-number((DEPTHTO div 2), '0.##')" />
                            <xsl:text>REC </xsl:text>
                            <xsl:value-of select="concat($Layer_mid,' ', REC_CARB_MRKR)" />
                            <xsl:text>&#xA;</xsl:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:if>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>END_CURVE  &#xA;</xsl:text>
        <xsl:text>END</xsl:text>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

i get that :

SOME 
TEXT 
Options 
REC 0.5 2
REC 2.5 2
END_CURVE  
END

As you can see "SOME TEXT Options" is always present, no matter if data is present or not (REC_CARB_MRKR node).

So id like to implement some routine, which checkts if in any node "Layer" the Node "REC_CARB_MRKR" exists, and if so, the "SOME TEXT Option" will be needed, also "END CURVE" will ne needed as well. At the moment it works great for some LAYER which do not have data. But when no LAYER contains REC_CARB_MKRK when the output useless and looks like:

SOME 
TEXT 
Options 
END_CURVE  
END

In that case the whole xslt block can be ignored, since no data exisits.

Any idea, how to acomplish that?

Thanks

CodePudding user response:

This in one possibility:

    <xsl:variable name="flagREC_CARB_MRKR" select="boolean(LAYERS/LAYER/REC_CARB_MRKR[1])"/>
    <xsl:if test="$flagREC_CARB_MRKR">
      <xsl:text>SOME &#xA;</xsl:text>
      <xsl:text>TEXT &#xA;</xsl:text>
      <xsl:text>Options &#xA;</xsl:text>
    </xsl:if>

And you could do this too:

   <xsl:if test="$flagREC_CARB_MRKR">
      <xsl:text>END_CURVE  &#xA;</xsl:text>
      <xsl:text>END</xsl:text>
      <xsl:text>&#xA;</xsl:text>
    </xsl:if>
  • Related