使用位置来计算节点 - 有时枚举从 0 而不是 1 开始

发布于 2024-11-08 05:10:19 字数 3549 浏览 1 评论 0原文

嘿伙计们, 我有一段相当笨拙的 xslt,我用它来将 SOAPUI 测试用例转换为更易读的格式。目前,它使用以下

我的问题 枚举测试用例有时,测试用例枚举会开始从 0 开始,有时从 1 开始。我不明白为什么会发生这种情况?这与位置选择器的实现方式有关吗?有没有更简洁的方法来计算节点的实例数?

非常感谢,

Richard

这是完整的代码 - 无样式。

`

  <!-- Edited by XMLSpy® -->
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:con="http://eviware.com/soapui/config">
    <xsl:output method="html" encoding ="utf-8"/>
    <xsl:template match="/">
      <html>
        <head>
          <script type="text/javascript">
            function toggleDiv(divid){
                var ele = document.getElementById(divid);

                if(ele.style.display == 'none')
                    { 
                        ele.style.display = 'block';
                    }
                else
                    {
                        ele.style.display = 'none';
                    }
                }
          </script>  

            <style type="text/css"></style>

        </head>

        <body>

          <xsl:apply-templates/>

          <div class="help">This report is generated automatically by a scheduled job running on Richard Fortune's machine. The SOAPUI project files it references are located in sourcecontrol SVN (https://svn.xxx.xxxxxx.com/svn/network/TEST). These reports are generated daily as the projects they reference are subject to change.</div>

        </body>
      </html>

    </xsl:template>

    <xsl:template match="con:soapui-project">
     <div><h1>Project Name : <xsl:value-of select="@name"/></h1></div> 
      <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="con:testSuite">    

    <xsl:if test="con:description=''">
                <p class="warn"> (RICHARD - PLEASE PROVIDE A DESCRIPTION!!)</p>
    </xsl:if>       
      <div id="content" onmousedown="toggleDiv('{position()}');"><h2 class="ex">TestSuite:  <xsl:value-of select="@name"/></h2></div>
      <br>
        <p class="descSuite"><b>Suite Description: </b><xsl:value-of select="con:description"/></p>
      </br>

      <div style="display:none" id="{position()}"><xsl:apply-templates />
      </div>
    </xsl:template>


    <xsl:template match="con:testCase">

        <ul>
          <li class="tc"><b>
            (#<xsl:value-of select="position()-3"/>) Testcase: </b><xsl:value-of select="@name"/>
          </li>

          <xsl:if test="con:description=''">
            <p class="warn">(Gentle reminder Richard - PLEASE PROVIDE A DESCRIPTION!!)</p>
          </xsl:if>

          <p class="descTc">
            <strong><i>Description:</i></strong> <xsl:value-of select="con:description"/>
          </p>
          <ol class="step">
          <xsl:for-each select="con:testStep"><li>TestStep: <xsl:value-of select="@name"/> </li></xsl:for-each>
          </ol>

        </ul>
        <xsl:apply-templates />

    </xsl:template>


    <xsl:template match="*"></xsl:template>


  </xsl:stylesheet>

`

Hey folks,
I've got this rather clumbsy piece of xslt which I use to transform SOAPUI test cases into a more readable format. Currently it enumerates the test cases using the following

<xsl:value-of select="position()-3"/>

My question Sometimes the test case enumeration starts at 0 and sometimes it starts at 1. I don't understand why this is happening? Is it something to do with how the position selector is implemented? Is there a neater way to count instances of a node?

Many thanks,

Richard

Here is the code in its entirety - sans styling.

`

  <!-- Edited by XMLSpy® -->
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:con="http://eviware.com/soapui/config">
    <xsl:output method="html" encoding ="utf-8"/>
    <xsl:template match="/">
      <html>
        <head>
          <script type="text/javascript">
            function toggleDiv(divid){
                var ele = document.getElementById(divid);

                if(ele.style.display == 'none')
                    { 
                        ele.style.display = 'block';
                    }
                else
                    {
                        ele.style.display = 'none';
                    }
                }
          </script>  

            <style type="text/css"></style>

        </head>

        <body>

          <xsl:apply-templates/>

          <div class="help">This report is generated automatically by a scheduled job running on Richard Fortune's machine. The SOAPUI project files it references are located in sourcecontrol SVN (https://svn.xxx.xxxxxx.com/svn/network/TEST). These reports are generated daily as the projects they reference are subject to change.</div>

        </body>
      </html>

    </xsl:template>

    <xsl:template match="con:soapui-project">
     <div><h1>Project Name : <xsl:value-of select="@name"/></h1></div> 
      <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="con:testSuite">    

    <xsl:if test="con:description=''">
                <p class="warn"> (RICHARD - PLEASE PROVIDE A DESCRIPTION!!)</p>
    </xsl:if>       
      <div id="content" onmousedown="toggleDiv('{position()}');"><h2 class="ex">TestSuite:  <xsl:value-of select="@name"/></h2></div>
      <br>
        <p class="descSuite"><b>Suite Description: </b><xsl:value-of select="con:description"/></p>
      </br>

      <div style="display:none" id="{position()}"><xsl:apply-templates />
      </div>
    </xsl:template>


    <xsl:template match="con:testCase">

        <ul>
          <li class="tc"><b>
            (#<xsl:value-of select="position()-3"/>) Testcase: </b><xsl:value-of select="@name"/>
          </li>

          <xsl:if test="con:description=''">
            <p class="warn">(Gentle reminder Richard - PLEASE PROVIDE A DESCRIPTION!!)</p>
          </xsl:if>

          <p class="descTc">
            <strong><i>Description:</i></strong> <xsl:value-of select="con:description"/>
          </p>
          <ol class="step">
          <xsl:for-each select="con:testStep"><li>TestStep: <xsl:value-of select="@name"/> </li></xsl:for-each>
          </ol>

        </ul>
        <xsl:apply-templates />

    </xsl:template>


    <xsl:template match="*"></xsl:template>


  </xsl:stylesheet>

`

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

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

发布评论

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

评论(3

满身野味 2024-11-15 05:10:20

我建议您使用 count()

<xsl:value-of select="count(preceding-sibling::*)+1"/>

这将返回节点相对于其所有兄弟元素(从 1 开始)的当前位置。

根据上下文,position() 可能不是正确的方法。

I suggest you to use count():

<xsl:value-of select="count(preceding-sibling::*)+1"/>

This returns the current position of the node respect to all its sibling elements (1-based).

position() might be not the proper way depending on the context.

甜味超标? 2024-11-15 05:10:20

以下是使用 的方法,它比使用 count() 更有效:

<li class="tc">
  <b>(#<xsl:number/>) Testcase: </b><xsl:value-of select="@name"/>
</li>

就是这么简单!默认情况下, 计算与上下文节点相同节点类型(在本例中为元素)和相同元素名称的节点...这就是您想要的,我相信。因此它将计算 元素。

Here is how you would use <xsl:number>, which is more efficient than using count():

<li class="tc">
  <b>(#<xsl:number/>) Testcase: </b><xsl:value-of select="@name"/>
</li>

It's that simple! By default, <xsl:number> counts the nodes of the same node kind (in this case, elements) and the same element name as the context node... which is what you want here, I believe. So it will count <con:testCase> elements.

找个人就嫁了吧 2024-11-15 05:10:20

position() 的问题是它可以(并且经常)包含您不期望的节点,例如文本甚至空白节点,如果读取的 XML 保留了它的话。

根据您的具体要求,您可以计算前面同级的数量(如 @empo 所描述),或者可能值得研究 指令。

The problem with position() is it can (and frequently does) include nodes you don't expect, such as text or even whitespace nodes, if whatever read the XML preserved it.

Depending on your precise requirements, you can count the number of preceding siblings (as @empo described), or it might be worth looking into the <xsl:number> instruction.

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