需要编写 XSpec 测试用例来测试 XSLT,其中使用多种模式进行转换。但是对于下面的测试用例,xspec 只测试应用默认模式的输出。我想知道是否有办法测试转换的最终输出。
<!-- 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>
<!-- 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>
O\P 首先<p>
:
--应用默认模式后:<p class="Title" text-align="center">
. [下面的 xspec 测试这个 o\p]
——最终:<title text-align="center">
。[想测试这个o\p]
<!-- 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>
在这方面的任何建议都会有很大帮助。谢谢...