使用xpath在xslt中循环
我想在一个xml文件中循环,循环时会显示所有值,除了//Script2[@time]它位于不同的位置,所以我直接选择了这样的值,出于某种原因,我不知道//Script2[@time]没有显示任何值使用xpath在xslt中循环,xslt,xpath,Xslt,Xpath,我想在一个xml文件中循环,循环时会显示所有值,除了//Script2[@time]它位于不同的位置,所以我直接选择了这样的值,出于某种原因,我不知道//Script2[@time]没有显示任何值 <table class="table_styling3"> <tr><th>Name</th><th>Duration</th><th>Errors</th><th>Actions<
<table class="table_styling3">
<tr><th>Name</th><th>Duration</th><th>Errors</th><th>Actions</th><th>Verifications</th> <th>Traces</th></tr>
<xsl:for-each select="Report/Scripts/Script1">
<tr>
<td widht="20%"><xsl:value-of select="@File"/></td>
<td widht="20%"><xsl:value-of select="//Script2[@time]"/></td>
<td width="10%"><xsl:value-of select="count(Message[@Type='Error'])"/></td>
<td width="10%"><xsl:value-of select="count(Message[@Type='Action'])"/></td>
<td width="10%"><xsl:value-of select="count(Message[@Type='Verification'])"/></td>
<td width="10%"><xsl:value-of select="count(Message[@Type='Trace'])"/></td>
</tr>
</xsl:for-each>
</table>
NameDurationErrorsActionsVerifications跟踪
我的xml文件在这里
<?xml version="1.0" encoding="utf-8"?>
<Report App="RIATest" Version="4.7" Project="D:\Projects\Automation\RIATest\Nomura\Nomura.rtp">
<Startup>
<Message Time="2012-08-30 15:32:38.578" Type="Info" Message="Launching iexplore.exe "http://mobiuss-dev.operasolutions.com:8080/nomura-test/""/>
<Message Time="2012-08-30 15:32:38.625" Type="Info" Message="Application launched (process id=4108)."/>
<Message Time="2012-08-30 15:32:38.625" Type="Info" Message="Waiting for connection from agent..."/>
<Message Time="2012-08-30 15:32:46.453" Type="Info" Message="Agent connection accepted. Application id=nomura-ui, index=0"/>
</Startup>
<Scripts>
<Script1 File="Login.rts" Time="2012-08-30 15:32:46.468">
<Message Time="2012-08-30 15:32:46.531" Type="Verification" Line="8" Col="58" Message="Verification passed: SparkApplication("nomura-ui")=>visible is equal to true"/>
<Message Time="2012-08-30 15:32:46.578" Type="Verification" Line="10" Col="50" Message="Verification passed: FlexForm("loginForm")=>visible is equal to true"/>
<Message Time="2012-08-30 15:32:46.625" Type="Verification" Line="12" Col="65" Message="Verification passed: FlexLabel("Username::Username:")=>text is equal to "Username:""/>
<Message Time="2012-08-30 15:32:46.656" Type="Verification" Line="14" Col="65" Message="Verification passed: FlexLabel("Password::Password:")=>text is equal to "Password:""/>
<Message Time="2012-08-30 15:32:46.671" Type="Verification" Line="17" Col="39" Message="Verification passed: comp=>label is equal to "Log In""/>
<Message Time="2012-08-30 15:32:46.703" Type="Verification" Line="18" Col="35" Message="Verification passed: comp=>enabled is equal to true"/>
<Message Time="2012-08-30 15:32:46.718" Type="Verification" Line="19" Col="35" Message="Verification passed: comp=>visible is equal to true"/>
<Message Time="2012-08-30 15:32:46.734" Type="Verification" Line="22" Col="35" Message="Verification passed: comp=>enabled is equal to true"/>
<Message Time="2012-08-30 15:32:46.750" Type="Verification" Line="23" Col="54" Message="Verification passed: comp=>label is equal to "Forgot your password?""/>
<Message Time="2012-08-30 15:32:46.765" Type="Verification" Line="24" Col="36" Message="Verification passed: comp=>selected is equal to false"/>
<Message Time="2012-08-30 15:32:46.781" Type="Verification" Line="25" Col="35" Message="Verification passed: comp=>visible is equal to true"/>
<Message Time="2012-08-30 15:32:46.796" Type="Verification" Line="28" Col="61" Message="Verification passed: comp=>text is equal to "CORPORATE BOND SPREAD SIGNAL""/>
<Message Time="2012-08-30 15:32:46.812" Type="Verification" Line="29" Col="35" Message="Verification passed: comp=>visible is equal to true"/>
<Message Time="2012-08-30 15:32:46.906" Type="Action" Line="32" Col="64" Message="SparkTextInput("Username::userName")=>textSelectionChange(0,0)"/>
<Message Time="2012-08-30 15:32:47.000" Type="Action" Line="33" Col="63" Message="SparkTextInput("Username::userName")=>textInput("arun.sekar")"/>
<Message Time="2012-08-30 15:32:47.031" Type="Action" Line="35" Col="61" Message="SparkTextInput("Password::password")=>textInput("opera100")"/>
<Message Time="2012-08-30 15:32:47.093" Type="Action" Line="36" Col="32" Message="SparkButton("Log In")=>click()"/>
</Script>
</Scripts>
<Results>
<Summary scripts="2" time="8.515" errors="2"/>
<Script2 name="Login.rts" time="0.625" errors="0"/>
<Script2 name="NOM_0002.rts" time="50.000" errors="2" message="Verification failed: comp=>selected is not equal to expected value true, it is actually equal to false"/>
</Results>
</Report>
在第一行和第二行显示时间0.625,在表中显示空行,所有其他值按预期显示 您当前正在XSLT中执行此操作
<xsl:value-of select="//Script2[@time]"/>
但是,这将获得第一个Script2标记的文本值,该标记恰好具有@time@属性。我认为您实际上想要获得这个@time属性的值。在这种情况下,请这样做
<xsl:value-of select="//Script2/@time"/>
但是,通过查看XSLT,您可能希望得到Script2标记,该标记恰好与Script1标记的@File属性具有相同的@name
<xsl:value-of select="//Script2[@name=current()/@File]/@time"/>
试试这个样式表
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<table class="table_styling3">
<tr>
<th>Name</th>
<th>Duration</th>
<th>Errors</th>
<th>Actions</th>
<th>Verifications</th>
<th>Traces</th>
</tr>
<xsl:for-each select="Report/Scripts/Script1">
<tr>
<td width="20%">
<xsl:value-of select="@File"/>
</td>
<td width="20%">
<xsl:value-of select="//Script2[@name=current()/@File]/@time"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Error'])"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Action'])"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Verification'])"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Trace'])"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
名称
期间
错误
行动
核实
踪迹
当应用于示例XML时,将输出以下内容
<table class="table_styling3">
<tr>
<th>Name</th>
<th>Duration</th>
<th>Errors</th>
<th>Actions</th>
<th>Verifications</th>
<th>Traces</th>
</tr>
<tr>
<td width="20%">Login.rts</td>
<td width="20%">0.625</td>
<td width="10%">0</td>
<td width="10%">4</td>
<td width="10%">13</td>
<td width="10%">0</td>
</tr>
</table>
名称
期间
错误
行动
核实
踪迹
Login.rts
0.625
0
4.
13
0
实际上,在这里使用xsl:key查找script2元素会更有效
<xsl:key name="script" match="Script2" use="@name"/>
试试这个XSLT。注意,我还将xsl:for-each替换为xsl:apply-templates,这通常更符合XSLT的“精神”
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="script" match="Script2" use="@name"/>
<xsl:template match="/">
<table class="table_styling3">
<tr>
<th>Name</th>
<th>Duration</th>
<th>Errors</th>
<th>Actions</th>
<th>Verifications</th>
<th>Traces</th>
</tr>
<xsl:apply-templates select="Report/Scripts/Script1"/>
</table>
</xsl:template>
<xsl:template match="Script1">
<tr>
<td width="20%">
<xsl:value-of select="@File"/>
</td>
<td width="20%">
<xsl:value-of select="key('script',@File)/@time"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Error'])"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Action'])"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Verification'])"/>
</td>
<td width="10%">
<xsl:value-of select="count(Message[@Type='Trace'])"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
名称
期间
错误
行动
核实
踪迹
是的,我们需要查看您的Xml文件,以及实际和预期的输出。在哪里?我可以看到“我的xml文件在这里”。啊,现在它在那里了。那么,实际结果和预期结果如何?@O.R.Mapper无法聊天,因为在我的办公室,他们禁用了聊天功能。请在这里回复。谢谢:)非常好的答案,正是我想要的,谢谢@Tim C,谢谢你过来,花时间理解我的问题并有效地解决它。再次感谢:)