【问题标题】:XML/XSL Help <xsl:for-each>XML/XSL 帮助 <xsl:for-each>
【发布时间】:2019-09-19 21:30:19
【问题描述】:

我正在尝试使用循环遍历我的元素,但它不断重复第一个元素,而不是遍历所有元素。

这是我的 xml 代码的 sn-p:

<?xml version="1.0" encoding="ISO-8859-1"?>
<customer ref="Hannah">

<fullname>

    <firstname>Hannah</firstname>

    <lastname>Hannah</lastname>

</fullname>

<meter ref="Hannah">

 <reading>

    <meterread>622224492</meterread>

</reading>

    </meter>

</customer>

<customer ref="Hannah">

<fullname>

    <firstname>Hannah</firstname>

    <lastname>Hannah</lastname>

</fullname>

<meter ref="Hannah">

<reading>

    <meterread>2822222132</meterread>

</reading>

</meter>

</customer> 

这是我的 XSL 代码的 sn-p:


<xsl:for-each select="customer">
<p>Name: <xsl:value-of select="customer/fullname"/></p>

<p>Num: <xsl:value-of select="customer//@ref"/></p>

</xsl:for-each>

基本上,它会不断重复第一个客户,而不是循环遍历每个元素。

任何帮助或建议都会很棒!

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: xslt xpath


【解决方案1】:

您的xsl:for-each 正在选择customer 元素,因此在xsl:for-each 内,您将定位在customer 元素上。这意味着您在 xsl:value-of 语句中的 XPath 表达式需要相对于它,而不是绝对路径。

例如,要获取全名,只需执行此操作,以获取相对于当前 customer 元素的 fullname 元素

<xsl:value-of select="fullname"/>

【讨论】:

  • 非常感谢。这行得通。我正在尝试在文档的另一部分应用相同的概念/承担,因为我遇到了与此类似的另一个问题。但是,它不适用:(
【解决方案2】:

只更改以下代码:-

<p>Name: <xsl:value-of select="$customerInfo//customer/fullname"/></p>
                       to
<p>Name: <xsl:value-of select="fullname"/></p>

                       and

<p>Account Number: <xsl:value-of select="$customerInfo/customer//@number"/></p>
                       to
<p>Account Number: <xsl:value-of select="@number"/></p>

【讨论】:

    猜你喜欢
    • 2012-07-20
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多