bigneo
bigneo

Reputation: 141

Execute condition in for-each loop only once (break for-each) in XSLT

I'm very new to XSLT and have this problem. I have found many posts and material that there is no way of breaking for-each loop in XSLT and variables cannot be changed after you declare them, but I couldn't find the solution to my problem. If there is one already answered, please provide a link.

I want to check if in a given node-set exists at least one element that contains a specific value. If it does, I want to create an element with an attribute ONLY ONCE in the transform.

<xsl:variable name="name" select="$path/to/something"/>
    <xsl:if test="count($name) > 0">
        <xsl:for-each select="$name">
            <xsl:if test="ctvf:trim(./@name) = 'P'">
                <name>
                    <xsl:attribute name="Name">
                        <xsl:value-of select="ctvf:trim(./@name)"/>
                    </xsl:attribute>
                </name>
            </xsl:if>
        </xsl:for-each>
    </xsl:if>

Currently if condition check is met <xsl:if test="ctvf:trim(./@name) = 'P'"> more than once, it creates several the same elements, I only need to create that element once.

Sample XML

<names>
    <name name="P"></name>
    <name name="B"></name>
    <name name="A"></name>
    <name name="P"></name>
</names>

Expected output

<name Name="P"></name>

Current Output

<name Name="P"></name>
<name Name="P"></name>

What would be simple solution to this?

Upvotes: 1

Views: 2613

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

I need to loop all elements and check weather it contains specific value, if it does I want to create an element with attribute. But I want to create element with that attribute only once, regardless if loop continues and more conditions are met afterwards.

Restate your problem as follows:

I want to check if in a given node-set exists at least one element that contains a specific value. If it does, I want to create an element with an attribute.

Which, after adjusting your example, would probably look something like this:

<xsl:variable name="name" select="$path/to/something"/>
<xsl:if test="$name[ctvf:trim(@name) = 'P']">
    <name Name="P"/>
</xsl:if>

Added:

To demonstrate, consider the following stylesheet:

XSLT 1.0

<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="/names">
    <xsl:if test="name[@name = 'P']">
        <name Name="P"/>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<name Name="P"/>

Applied to this example:

<names>
    <name name="X"></name>
    <name name="B"></name>
    <name name="A"></name>
    <name name="Y"></name>
</names>

the result will be empty.

Another way to achieve the same thing would be:

<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:strip-space elements="*"/>

<xsl:template match="name[@name = 'P'][1]">
    <name Name="P"/>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions