开发者

Loop through XML elements in XSL

I have in XML file like this

<ViewFields> 
<FieldRef Name="Approval Status" /> 
<FieldRef Name="Requirement Status" /> 
<FieldRef Name="Development Status" /> 
<FieldRef Name="Testing Status" />
</ViewStatus>

I have the following XSL code to get FieldRef values.

<xsl:template name="Fiel开发者_运维技巧dRef_body.Status" match="FieldRef[@Name='ViewFields/FieldRef[1]/@Name']" mode="body">
        <xsl:param name="thisNode" select="."/>
            <xsl:choose>
                <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'Completed'">
                    <img src="/_layouts/images/IMNON.png" alt="Status: {$thisNode/@Status}"/>
                </xsl:when>
                <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'In Progress'">
                    <img src="/_layouts/images/IMNIDLE.png" alt="Status: {$thisNode/@Status}"/>
                </xsl:when>
                <xsl:otherwise>
                    <img src="/_layouts/images/IMNBUSY.png" alt="Status: {$thisNode/@Status}"/>
                </xsl:otherwise>
            </xsl:choose>
    </xsl:template>

I am trying to LOOP through FieldRef*[x]* to get the values one by one, it's not returning anything. I want to assign FieldRef values to @Name variable through loop.


This is obvious:

  1. match="FieldRef[@Name='ViewFields/FieldRef[1]/@Name']". No Name attribute has as string value the string 'ViewFields/FieldRef[1]/@Name'. You most probably want an XPath expression here, not a string. Use match="FieldRef[@Name=ViewFields/FieldRef[1]/@Name]"

  2. There is no Name attribute in the provided XML document with string value "Completed" or with string value "In Progress".

  3. Also, there isn't any Status attribute at all in the XML document.


Your question doesn't have all of the context needed to answer correctly, but you should consider simplifying your construct down to a "for-each" for your looping.

given xml

<ViewFields> 
<FieldRef Name="Approval Status" />
<FieldRef Name="Requirement Status" /> 
<FieldRef Name="Development Status" /> 
<FieldRef Name="Testing Status" />
</ViewFields>

with xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="main" match="/">
    <xsl:for-each select="/ViewFields/FieldRef">
        <xsl:choose>
            <xsl:when test="@Name = 'Approval Status'">
                <ApprovalStatus/>
            </xsl:when>
            <xsl:when test="@Name = 'Requirement Status'">
                <RequirementStatus/>
            </xsl:when>
            <xsl:otherwise>
                <SomethingElse/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>

This might be a bit closer to what you were wanting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜