首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用XSpec测试具有多种模式的XSLT?

如何用XSpec测试具有多种模式的XSLT?
EN

Stack Overflow用户
提问于 2020-07-08 04:00:42
回答 2查看 206关注 0票数 0

需要编写XSpec测试用例来测试XSLT,其中使用多种模式进行转换。但是对于下面的测试用例,xspec只测试默认模式下的输出。我想知道是否有一种方法来测试转换的最终输出。

代码语言:javascript
复制
<!-- input.xml -->
<body>
 <div>
   <p class="Title"><span>My first title</span></p>
   <p class="BodyText"><span style="font-weight:bold">AAAAAAA</span><span>2 Jan 2020</span></p>
 </div>
</body>
代码语言:javascript
复制
<!-- conv.xsl -->
<xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

<!-- default mode : adding text-align attribute where @class=Title -->
<xsl:template match="*[ancestor::body]">
        <xsl:choose>
            <xsl:when test="@class = 'Title'">
                <xsl:element name="{local-name()}">
                    <xsl:copy-of select="@* except @style"/>
                    <xsl:attribute name="text-align" select="'center'"/>
                    <xsl:apply-templates/>
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{local-name()}">
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

<!-- bodytext mode : changing element name to <title> where p[@class=Title] -->
<xsl:template match="p[@class]" mode="bodytext">
        <xsl:choose>
            <xsl:when test="@class = 'Title'">
                <title>
                    <xsl:copy-of select="@* except @class"/>
                    <xsl:apply-templates mode="bodytext"/>
                </title>
            </xsl:when>
            <xsl:otherwise>
              <para>
                    <xsl:apply-templates mode="bodytext"/>
              </para>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

<xsl:template match="body">
        <xsl:variable name="data">
            <body>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates/>
            </body>
        </xsl:variable>
        <xsl:apply-templates select="$data" mode="bodytext"/>
    </xsl:template>

<xsl:template match="node() | @*" mode="#all">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" mode="#current"/>
        </xsl:copy>
    </xsl:template>

第一次<p>的O\P

-默认模式应用后:<p class="Title" text-align="center">。下面xspec测试这个o\p

-决赛:<title text-align="center">。想要测试这个o\p

代码语言:javascript
复制
<!-- test.xspec -->
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" stylesheet="conv.xsl">
  <x:scenario label="XSS00001: Testing 'p[@class=Title]' converts to 'title'">
     <x:context href="input.xml" select="/body/div[1]/p[1]"/>
     <x:expect label="Testing 'p' converts to 'title'">
        <title text-align="center">
           <span>My first title</span>
        </title>
     </x:expect>
  </x:scenario>
</x:description>

这方面的任何建议都会有很大帮助。谢谢..。

EN

回答 2

Stack Overflow用户

发布于 2020-07-09 14:58:48

我不认为仅仅是模式的使用没有给出你想要的结果。但是,按照在XSLT中设置模式的方式,如果您在XSpec测试场景中匹配该XSpec,那么您将得到仅应用于该p元素的样式表。显然,对于该p*[ancestor::body]上的匹配处于未命名模式中,并且处理在该模式下停止,因为其他模式从未从该模板中使用。

因此,您可能需要使body元素成为上下文,并使用如下场景:

代码语言:javascript
复制
<x:scenario label="XSS00002: Testing 'p[@class=Title]' converts to 'title'">
    <x:context>
        <body>
            <div>
                <p class="Title">...</p>
                <p class="BodyText">...</p>
            </div>
        </body>
    </x:context>
    <x:expect label="Testing 'p' converts to 'title'">
        <body>
            <div>
                <title text-align="center">...</title>
                <para>...</para>
            </div>
        </body>
    </x:expect>
</x:scenario>
票数 0
EN

Stack Overflow用户

发布于 2020-07-14 13:41:30

马丁说得很对。

另一种写作方式是:

代码语言:javascript
复制
  <x:scenario label="When a document contains 'body//p[@class=Title]'">
     <x:context href="input.xml" />
     <x:expect label="'p' is converted to 'title[@text-align]'"
               test="body/div/title">
        <title text-align="center">
           <span>My first title</span>
        </title>
     </x:expect>
  </x:scenario>

那是,

(/).

  • Add

  • x:context中删除@select,因为您和/或conv.xsl似乎假定转换总是从文档节点@test开始到x:expect,因为您似乎只对转换结果中的title元素感兴趣。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62787346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档