【问题标题】:xsl for-each loop not working correctly(logic)xsl for-each 循环无法正常工作(逻辑)
【发布时间】:2013-12-16 17:35:08
【问题描述】:

我在 xsl for-each 循环中应用了一个逻辑,但它的行为不正确。谁能帮帮我。

XML

<a>
 <b>
   <c>
     <string>16</string>
     <string>4</string>
     <string>id</string>
     <int>123</int>
   </c>
   <c>
     <string>16</string>
     <string>4</string>
     <string>id</string>
     <int>123</int>
  </c>
</b>
</a>

XSL

<xsl:for-each select="/a/b/c">
   <c>      
    <xsl:for-each select="/a/b/c/string">"
        <xsl:variable name ="pos" select="position()"/>
        <xsl:if test="position() mod 2!=0">
            <xsl:choose>
               <xsl:when test="self::node()[text()='16']">
                   <int>16</int><int>4</int>
               </xsl:when>
               <xsl:otherwise>
                    <xsl:element name="{/a/b/c/string[position()=$pos]}">"
                         <xsl:value-of select="/a/b/c/string[position()=$pos+1]\/>
                    </xsl:element>
               </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:for-each>
</c>        </xsl:for-each>

期望的输出

  <a>
     <b>
       <c>
         <int>16</int>
         <int>4</int>
         <id>123</id>
      </c>
     <c>
        <int>16</int>
        <int>4</int>
        <id>123</id>
    </c>
  </b>
</a>

实际输出

<a>
<b>
<c>
<int>16</int>
<int>4</int>
<id>123</id>
<int>16</int>
<int>4</int>
</c>
<c>
<int>16</int>
<int>4</int>
<id>123</id>
<int>16</int>
<int>4</int>
</c>
</b>
</a>

内部 for-each 循环存在一些问题,但我无法找到

【问题讨论】:

  • 您提出问题的方式表明存在心态问题。如果一个程序没有达到你的预期,那么你的预期有 99.9% 的可能性是错误的。首先说“我犯了一个错误”,而不是“程序行为不正确”,然后您立即将自己定位在正确的位置开始寻找原因。
  • 对不起@MichaelKay,但我写的 xsl for-each 循环不能正常工作(逻辑)。我到处提到我已经应用了一个逻辑,但这个逻辑对我来说不起作用,我清楚地说它只是我的错误,而不是 prog 的...... :) 无论如何,我会记住你的建议。
  • 也许只是用英语吧。该程序根据语言规范正常工作。这并不意味着它正在做你想做的事情。

标签: xslt foreach iteration transform


【解决方案1】:

这是你的内部 for-each 循环

 <xsl:for-each select="/a/b/c/string">

当 xpath 表达式以 "/" 开头时,表示它是一个绝对表达式。 “/”指的是顶级文档节点,无论您当前在 XML 中的哪个位置,它都会从 XML 的根目录开始选择内容。

你想要的是一个相对的表达。在你做你的内部“for-each”时,你当前的上下文是“c”元素,所以你需要写的就是这个

<xsl:for-each select="string">

这将只返回当前“c”元素的子元素“string”元素。

此外,您当前的 xsl:element 语句随后可以更改。而不是这样做

<xsl:element name="{/a/b/c/string[position()=$pos]}">

你可以这样做

<xsl:element name="{.}">

要在此时获取以下元素的值,请执行此操作。

<xsl:value-of select="following-sibling::*[1]"/>

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" />

    <xsl:template match="/">
    <xsl:for-each select="a/b/c">
       <c>      
        <xsl:for-each select="string">
            <xsl:if test="position() mod 2!=0">
                <xsl:choose>
                   <xsl:when test="self::node()[text()='16']">
                       <int>16</int><int>4</int>
                   </xsl:when>
                   <xsl:otherwise>
                        <xsl:element name="{.}">
                             <xsl:value-of select="following-sibling::*[1]"/>
                        </xsl:element>
                   </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
        </xsl:for-each>
        </c>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2012-10-11
    • 2023-03-25
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多