XSLT检查属性值长度是否为0

时间:2016-06-22 10:55:08

标签: xml xslt

我想根据属性值的长度过滤XML,但是很少有人知道。我的申请是<xsl:when>正确吗?

即使Description的长度为0,我也没有按预期得到输出。

我的XML是

<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>

我的XSLT

<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>

1 个答案:

答案 0 :(得分:2)

在以下表达式中

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

子表达式

ReportAttribute//@AttrValue

返回包含所有 AttrValue元素的ReportAttribute属性的节点集,尤其是ReportAttribute AttrName="Name"。您应该将测试更改为

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