Sahal
Sahal

Reputation: 4146

Get multiple attributes values from XML element using XSLT

I have an XML like below

<Screenings>
    <Screening type="EFG" desc="Financial Report">
        <ScreeningStatus>
            <OrderStatus>Complete</OrderStatus>
            <ResultStatus>Review</ResultStatus>
        </ScreeningStatus>
    </Screening>
    <Screening type="EFG" desc="Financial Report">
        <ScreeningStatus>
            <OrderStatus>Complete</OrderStatus>
            <ResultStatus>Fail</ResultStatus>
        </ScreeningStatus>
    </Screening>ngStatus>
    </Screening>
    <Screening subtype="CARG" type="ABCD"  desc="registry search">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
    <Screening subtype="CARG" type="ABCD"  desc="registry search">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
    <Screening subtype="KARG" type="ABCD">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
    <Screening subtype="KARG" type="ABCD"  desc="registry search">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
</Screenings>

I need a string like below (Get the unique type and subtype attributes)

EFG-|ABCD-CARG|ABCD-KARG

Then split this by pipe | and loop through it. Inside loop I need type and subtype split by hyphen (-) Need two variables for type and subtype - like below

for (split by pipe | val : array) {
       split by hyphen and create two variable for type and subtype
       (ABCD-KARG)
       var type=ABCD
       var subtype=KARG
       // I have some business logic to do here
}

I tried this -

<xsl:variable name="typeSubTypeArray" select="string-join(./ns0:Screening/@type, ',')"/>

But I'm not able to get add the subtype value to it and create a unique combination value string

Also if it only type and no subtype the i'm getting unique value using this -

<xsl:for-each select="distinct-values(./ns0:Screening/@type))">

But how do I get the unique value using type/subtype combination. I need the attribute values.

Upvotes: 0

Views: 856

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117100

If I understand your question correctly, you could do:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/Screenings">
    <xsl:variable name="groups">
        <xsl:for-each-group select="Screening" group-by="concat(@type, '-', @subtype)">
            <group key="{current-grouping-key()}"/>
        </xsl:for-each-group>
    </xsl:variable>
    <xsl:value-of select="$groups/group/@key" separator="|"/>
</xsl:template>

</xsl:stylesheet>

Or just :

<xsl:template match="/Screenings">
    <xsl:value-of select="distinct-values(Screening/concat(@type, '-', @subtype))" separator="|"/>
</xsl:template>

Upvotes: 1

Tim C
Tim C

Reputation: 70648

Are you over-complicating the process here? I think you can just use xsl:for-each-group to get the values you need, without the need for building up a string then splitting it.

<xsl:for-each-group select="Screening" group-by="@type">
  <xsl:for-each-group select="current-group()" group-by="string(@subtype)">
    <xsl:value-of select="concat('Processing ', @type, ' - ', @subtype, '&#10;')" />
  </xsl:for-each-group>
</xsl:for-each-group>

Note that, if you really did want to create your pipe-delimited string, you would do this...

<xsl:variable name="distinct" select="string-join(distinct-values(Screening/concat(@type, '-', @subtype)), '|')" />

Upvotes: 2

Related Questions