在 XSL 中循环 XML 元素
我在这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是显而易见的:
match="FieldRef[@Name='ViewFields/FieldRef[1]/@Name']"
。没有Name
属性将字符串'ViewFields/FieldRef[1]/@Name'
作为字符串值。您很可能需要一个 XPath 表达式,而不是字符串。使用match="FieldRef[@Name=ViewFields/FieldRef[1]/@Name]"
提供的 XML 文档中没有带有字符串值的
Name
属性“Completed”
或字符串值“In Progress”
。此外,XML 文档中根本不存在任何
Status
属性。This is obvious:
match="FieldRef[@Name='ViewFields/FieldRef[1]/@Name']"
. NoName
attribute has as string value the string'ViewFields/FieldRef[1]/@Name'
. You most probably want an XPath expression here, not a string. Usematch="FieldRef[@Name=ViewFields/FieldRef[1]/@Name]"
There is no
Name
attribute in the provided XML document with string value"Completed"
or with string value"In Progress"
.Also, there isn't any
Status
attribute at all in the XML document.您的问题没有正确回答所需的所有上下文,但您应该考虑将循环的构造简化为“for-each”。
给定 xml
和 xsl
这可能更接近您想要的。
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
with xsl
This might be a bit closer to what you were wanting.