在 XSL 中循环 XML 元素

发布于 2024-11-27 08:29:05 字数 1302 浏览 0 评论 0原文

我在这样的 XML 文件中

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

使用以下 XSL 代码来获取 FieldRef 值。

<xsl:template name="FieldRef_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>

我试图通过 FieldRef*[x]* 循环来一一获取值,它不返回任何内容。我想通过循环将 FieldRef 值分配给 @Name 变量。

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="FieldRef_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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女中豪杰 2024-12-04 08:29:05

这是显而易见的

  1. match="FieldRef[@Name='ViewFields/FieldRef[1]/@Name']"。没有 Name 属性将字符串 'ViewFields/FieldRef[1]/@Name' 作为字符串值。您很可能需要一个 XPath 表达式,而不是字符串。使用 match="FieldRef[@Name=ViewFields/FieldRef[1]/@Name]"

  2. 提供的 XML 文档中没有带有字符串值的 Name 属性“Completed” 或字符串值 “In Progress”

  3. 此外,XML 文档中根本不存在任何 Status 属性。

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.

躲猫猫 2024-12-04 08:29:05

您的问题没有正确回答所需的所有上下文,但您应该考虑将循环的构造简化为“for-each”。

给定 xml

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

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

这可能更接近您想要的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文