Home > front end >  Check for existence of specific child element in XSLT
Check for existence of specific child element in XSLT

Time:04-21

Using XSLT I am generating a table that contains a row for each ejerlejlighed-tag in XML (see below).

Path: forandringer->ejerlejligheder->ejerlejlighed

Each ejerlejlighed-tag contains a range of information that should be presented as columns, where one tag is optional: anvendelse.

The question:

My client has asked that a column for the anvendelse-tag is only shown if at least one of the ejerlejlighed-tags contain this information.

Path forandringer->ejerlejligheder->ejerlejlighed->anvendelse

    <forandringer>
    <aendrings>
        <changesOrder>1</changesOrder>
        <sagsoperationKode>EOPEF</sagsoperationKode>
        <sagsoperationBetegnelse>Oprettelse af ejerlejlighedsfællesskab</sagsoperationBetegnelse>
        <hovedejendomBFEnr>3330122</hovedejendomBFEnr>
        <hovedejendomType>SFE</hovedejendomType>
    </aendrings>
    <aendrings>
        <changesOrder>2</changesOrder>
        <sagsoperationKode>ENOPAH</sagsoperationKode>
        <sagsoperationBetegnelse>Anden hjemmel*</sagsoperationBetegnelse>
        <hovedejendomBFEnr>3330122</hovedejendomBFEnr>
        <hovedejendomType>SFE</hovedejendomType>
        <aktuelBFEnr>100229741</aktuelBFEnr>
        <aktuelEJLnr>1</aktuelEJLnr>
        <kommentar>§18</kommentar>
    </aendrings>
    <ejerlejligheder>
        <mainPropertyBFENumber>3330122</mainPropertyBFENumber>
        <ejerlejlighed>
            <BFENumber>100229741</BFENumber>
            <ejerlejlighedNr>1</ejerlejlighedNr>
            <adresse>Ukendt adresse</adresse>
            <areal>100</areal>
            <fordelingstal>25/100</fordelingstal>
        </ejerlejlighed>
        <ejerlejlighed>
            <BFENumber>100229742</BFENumber>
            <ejerlejlighedNr>2</ejerlejlighedNr>
            <adresse>Ukendt adresse</adresse>
            <anvendelse>Erhverv</anvendelse>
            <areal>200</areal>
            <fordelingstal>25/100</fordelingstal>
        </ejerlejlighed>
        <ejerlejlighed>
            <BFENumber>100229743</BFENumber>
            <ejerlejlighedNr>3</ejerlejlighedNr>
            <adresse>Ukendt adresse</adresse>
            <areal>200</areal>
            <fordelingstal>25/100</fordelingstal>
        </ejerlejlighed>
        <ejerlejlighed>
            <BFENumber>100229744</BFENumber>
            <ejerlejlighedNr>4</ejerlejlighedNr>
            <adresse>Ukendt adresse</adresse>
            <areal>200</areal>
            <fordelingstal>25/100</fordelingstal>
        </ejerlejlighed>
    </ejerlejligheder>
</forandringer>

To support this i have created a template that is called as below. However i don't know how to make the select statement for param show-anvendelse true or false based on whether an anvendelse-tag exists.

<xsl:call-template name="ejerlejlighed-table-only">
     <xsl:with-param name="lejligheder" select="current-group()"/>
     <xsl:with-param name="show-anvendelse" select="false()"/>
</xsl:call-template>

CodePudding user response:

Try exists(xpath) function:

<xsl:call-template name="ejerlejlighed-table-only">
     <xsl:with-param name="lejligheder" select="current-group()"/>
     <xsl:with-param name="show-anvendelse" select="exists(anvendelse)"/>
</xsl:call-template>

CodePudding user response:

The context of your question is not clear. There is nothing in your description that would indicate a need for grouping and/or for calling a named template.

If you are creating a row for each ejerlejlighed, and you want to create a specific column only when at least one ejerlejlighed has an anvendelse child, then obviously you want to make this test once for all the rows, and use the result in each row - here is a simplified example:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/forandringer">
    <xsl:variable name="anvendelse" select="ejerlejligheder/ejerlejlighed/anvendelse" />
    <table>
        <xsl:for-each select="ejerlejligheder/ejerlejlighed">
            <row>
                <!-- mandatory columns -->
                <xsl:if test="$anvendelse">
                    <optional-column>
                        <!-- some value -->
                    </optional-column>
                </xsl:if>
            </row>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

CodePudding user response:

You can count the number of anvendelse tags to see if there's at least a value:

<xsl:call-template name="ejerlejlighed-table-only">
     <xsl:with-param name="lejligheder" select="current-group()"/>
     <xsl:with-param name="show-anvendelse" select="count(//ejerlejlighed/anvendelse) &gt; 0"/>
</xsl:call-template>

It's not clear how you're calling the ejerlejlighed-table-only template, but try to use the most specific selector in the count to avoid performance penalties.

  • Related