kpgu1718
kpgu1718

Reputation: 97

XSLT check whether attribute value length is 0 or not

I want to filter XML based on length of attribute value, but for few attibutes. Is my apporach to <xsl:when> correct?

Even though length of Description is 0, I am not getting output as expected.

My XML is

<FBSItem>
    <ReportAttribute AttrName="Name" AttrValue="006260 FBS" />
    <ReportAttribute AttrName="Description" AttrValue="SPXRMTRequirementClass_Top" />
</FBSItem>
<FBSItem>
    <ReportAttribute AttrName="Name" AttrValue="006260A Galleggiare" />
    <RIItem>
        <ReportAttribute AttrName="Name" AttrValue="Test 0011" />
        <ReportAttribute AttrName="Description" AttrValue="Test 0011" />
    </RIItem>
    <RIItem>
        <ReportAttribute AttrName="Name" AttrValue="Test 0012" />
        <ReportAttribute AttrName="Description" AttrValue="" />
    </RIItem>
    <RIItem>
        <ReportAttribute AttrName="Name" AttrValue="Test 0013" />
        <ReportAttribute AttrName="Description" AttrValue="" />
    </RIItem>
</FBSItem>
<FBSItem>
    <ReportAttribute AttrName="Name" AttrValue="006260A1 Dislocare" />
    <RIItem>
        <ReportAttribute AttrName="Name" AttrValue="Test 001" />
        <ReportAttribute AttrName="Description" AttrValue="" />
    </RIItem>
</FBSItem>

My XSLT is

<xsl:for-each select="RIItem">
    <w:p w:rsidR="00DD44EB" w:rsidRPr="00DD44EB" w:rsidRDefault="00DD44EB">
        <w:pPr>
            <w:rPr>
                <w:b/>
                <w:sz w:val="28"/>
            </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="00DD44EB">
            <w:rPr>
                <w:b/>
                <w:sz w:val="28"/>
            </w:rPr>
            <w:t>
                <xsl:value-of select='ReportAttribute[@AttrName="Name"]/@AttrValue'/>
            </w:t>
        </w:r>
        <w:proofErr w:type="gramStart"/>
        <w:r w:rsidRPr="00DD44EB">
            <w:rPr>
                <w:b/>
                <w:sz w:val="28"/>
            </w:rPr>
            <w:t>
                <xsl:choose>
                    <xsl:when test="ReportAttribute//@AttrName ='Description' ">
                        <xsl:choose>
                            <xsl:when test="string-length(ReportAttribute//@AttrValue) > 0 ">
                                <xsl:text>;</xsl:text>
                                <xsl:value-of select='ReportAttribute[@AttrName="Description"]/@AttrValue'/>
                            </xsl:when>
                            <xsl:otherwise></xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise></xsl:otherwise>
                </xsl:choose>
            </w:t>
        </w:r>
    </w:p>
<xsl:for-each>

Upvotes: 0

Views: 1297

Answers (1)

nwellnhof
nwellnhof

Reputation: 33658

In the following expression

string-length(ReportAttribute//@AttrValue) > 0

the subexpression

ReportAttribute//@AttrValue

returns a nodeset containing the AttrValue attributes of all ReportAttribute elements, particularly the ReportAttribute with AttrName="Name". You should change the test to something like

string-length(ReportAttribute[@AttrName="Description"]/@AttrValue) > 0

Upvotes: 2

Related Questions