TalendDeveloper
TalendDeveloper

Reputation: 79

XSLT Transformation in databricks to get the value based on condition

I'm new to XSL programming and trying to perform the below transformation where i'll have to fetch the value based on a text value in other attribute/tag

Source XML:

<ns1:specificationSectionDetail>
    <ns1:specificationSectionFoodOtherLabellingCopySection>
        <ns1:claimOrStatement>
            <ns1:parameterisedEntity>
                <ns2:code>NUTRITION_CLAIMS_104</ns2:code>
                <ns2:localeData>
                    <ns1:description>
                        <![CDATA[Health Star Rating DI/Claims {l}]]>
                    </ns1:description>
                    <ns1:id>16601</ns1:id>
                </ns2:localeData>
            <ns1:packCopyText>Health Star Rating DI/Claims HSR - Test 72339</ns1:packCopyText>
            <ns1:useOnPackText>Back - Follow Brand Style Guide</ns1:useOnPackText>
            <ns1:statement>Health Star Rating DI/Claims  HSR - Test 72339</ns1:statement>
        </ns1:claimOrStatement>
        <ns1:claimOrStatement>
            <ns1:parameterisedEntity>
                <ns2:code>NUTRITION_CLAIMS_35</ns2:code>
                <ns2:localeData>
                    <ns1:description>
                        <![CDATA[Health Star Rating {s}]]>
                    </ns1:description>
                <ns1:packCopyText>Health Star Rating Better - 72339</ns1:packCopyText>
            <ns1:useOnPackText>Front - Follow Brand Style Guide</ns1:useOnPackText>
            <ns1:statement>Health Star Rating  Better - 72339</ns1:statement>
        </ns1:claimOrStatement>
        <ns1:claimOrStatement>
            <ns1:parameterisedEntity>
                <ns2:code>NUTRITION_CLAIMS_258</ns2:code>
                <ns2:localeData>
                    <ns1:description>
                        <![CDATA[Potassium contributes to normal functioning of the nervous system]]>
                    </ns1:description>
                    <ns1:id>18193</ns1:id>
                </ns2:localeData>
            <ns1:packCopyText>Potassium contributes to normal functioning of the nervous system</ns1:packCopyText>
            <ns1:useOnPackText>Back - Follow Brand Style Guide</ns1:useOnPackText>
            <ns1:statement>Potassium contributes to normal functioning of the nervous system</ns1:statement>
        </ns1:claimOrStatement>

XSLT File

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple" exclude-result-prefixes="ns1 ns1">
    <xsl:template match="health-star-rating">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text,'DI/Claims')">
            <xsl:value-of select="$text" />
        </xsl:when>
    </xsl:choose>
    </xsl:template>
    <xsl:template match="/">
        <ItemDetails>
            <SourceSystem>
                <xsl:text>Fusion</xsl:text>
            </SourceSystem>
            <ActionType>
                <xsl:text>Create</xsl:text>
            </ActionType>
            <CreateDateTime>
                <xsl:text>2021-11-10T08:00:00</xsl:text>
            </CreateDateTime>
            <Items>
                <Item>
                <HealthStarRating>
                        <xsl:value-of select="ns1:productSpecificationFullDTO/ns1:specificationSectionDetail/ns1:specificationSectionFoodOtherLabellingCopySection/ns1:claimOrStatement/ns1:packCopyText"/>
                </HealthStarRating>
                <HealthStarRatingDIClaims>
                        <xsl:value-of select="ns1:productSpecificationFullDTO/ns1:specificationSectionDetail/ns1:specificationSectionFoodOtherLabellingCopySection/ns1:claimOrStatement/ns1:packCopyText"/>
                </HealthStarRatingDIClaims>
                </Item>
            </Items>
        </ItemDetails>
    </xsl:template>
</xsl:stylesheet>

Transformed XML should look like below

<HealthStarRating>Health Star Rating Better - 72339</HealthStarRating>
<HealthStarRatingDIClaims>Health Star Rating DI/Claims HSR - Test 72339</HealthStarRatingDIClaims>
<Nutritions>
   <Instruction>
       Potassium contributes to normal functioning of the nervous system
    </Instruction>
</Nutritions>

Basically the XSLT should include a condition that determines the value of "description" tag and fetch the value of "Text" tag and outputs as a separate attributes

Thanks in advance

Upvotes: 0

Views: 263

Answers (1)

zx485
zx485

Reputation: 29022

You may use the following XSLT-1.0 template.

In a first step it extracts the "description" without spaces and stores it in a variable.
In the second step it checks whether the description is one of the specific types or an <Others>. If it's a specific type, it derives the element name from the description.

<xsl:template match="claims">
    <xsl:variable name="elemName" select="translate(entity/description,' &#xa;&#xd;','')" />
    <xsl:choose>
        <xsl:when test="$elemName='HealthStar' or $elemName='DIClaims'">
            <xsl:element name="{$elemName}">
                <xsl:value-of select="normalize-space(Text)" />
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <Others><xsl:value-of select="normalize-space(Text)" /></Others>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Its output is as desired.

Upvotes: 0

Related Questions