【发布时间】:2012-10-26 17:29:46
【问题描述】:
我正在尝试将 Apple 的 plist 文件转换为 JUnit XML 格式,以便 Hudson/Jenkins 可以读取结果。
我有一个类似这样的 plist 文件输出:
<plist><dict><array>
<dict>
<key>LogType</key>
<string>Pass</string>
<key>Message</key>
<string>Test 1</string>
<key>Timestamp</key>
<date>2012-10-26T09:42:41Z</date>
<key>Type</key>
<integer>4</integer>
</dict>
<dict>.....</dict>
.
.
.
<dict>
<key>LogType</key>
<string>Pass</string>
<key>Message</key>
<string>Test 1</string>
<key>Timestamp</key>
<date>2012-10-26T09:43:27Z</date>
<key>Type</key>
<integer>5</integer>
</dict>
<dict>
<key>LogType</key>
<string>Fail</string>
<key>Message</key>
<string>Inserting content to a group</string>
<key>Timestamp</key>
<date>2012-10-26T09:49:13Z</date>
<key>Type</key>
<integer>4</integer>
</dict>
<dict>.....</dict>
.
.
.
<dict>
<key>LogType</key>
<string>Error</string>
<key>Message</key>
<string>VerboseError: target.frontMostApp().mainWindow().buttons()[3] could not be tapped</string>
<key>Screenshot</key>
<string></string>
<key>Timestamp</key>
<date>2012-10-26T09:50:12Z</date>
<key>Type</key>
<integer>3</integer>
</dict>
<dict>
<key>LogType</key>
<string>Fail</string>
<key>Message</key>
<string>Inserting content to a group</string>
<key>Screenshot</key>
<string></string>
<key>Timestamp</key>
<date>2012-10-26T09:50:13Z</date>
<key>Type</key>
<integer>7</integer>
</dict>
</array></dict>
</plist>
我需要将其转换为 JUnit XML。这是我期望上面 plist 字段的输出:
<?xml version="1.0"?>
<testsuites>
<testsuite>
<testcase classname="Test 1" name="Test 1"/>
<testcase classname="Inserting content to a group" name="Inserting content to a group">
<failure>Inserting content to a group - VerboseError: target.frontMostApp().mainWindow().buttons()[3] could not be tapped< </failure>
</testcase>
</testsuite>
</testsuites>
目前我有这个 XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<testsuites>
<testsuite>
<xsl:for-each select="plist/dict/array/dict">
<xsl:if test="integer = 4">
<testcase>
<xsl:attribute name="classname"><xsl:value-of select="string[2]" /></xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="string[2]"/></xsl:attribute>
<xsl:if test="string[1] = 'Fail'">
<failure>
<xsl:attribute name="type"><xsl:value-of select="integer" /></xsl:attribute><xsl:value-of select="string[2]" />
</failure>
</xsl:if>
</testcase>
</xsl:if>
</xsl:for-each>
</testsuite>
</testsuites>
</xsl:template>
</xsl:stylesheet>
如何编辑上面的 XSL 以查找两个 dict/string[2] = 'Test A' 之间的任何错误消息并将消息插入到 <failure> 的值中?我不确定如何执行此操作,因为错误消息包含在另一个 <dict> 节点中。
编辑:
好的,我已将其分解为伪 ish 代码:
计算整数=4的所有节点。
找出每个整数=4的节点的位置,并存入一个变量
-
遍历整数 = 4 的每个节点,并在整数 = 4 的下一个节点之前找到任何 string[1] = 'Fail'。
-
如果有任何 string[1] = 'Fail' 则在下一个整数 = 4 的节点之前和上一个整数 = 4 的节点之后找到 string[1] = 'Error'。
- 如果任何 string[1] = 'Error',则输出
failure,字符串 [2] 来自当前节点,字符串 [2] 来自前一个节点,整数 = 4。
- 如果任何 string[1] = 'Error',则输出
- Else 输出
failure,字符串 [2] 来自前一个节点,整数 = 4。
-
节点引用plist/dict/array/dict
XSL 可以做到这一点吗?
【问题讨论】:
-
与其说您想要“使用 XSL 的类似的东西”,不如解释一下将源 XML 转换为所需结果的规则。
-
好吧,有点-但是您删除了所需输出的示例。 :) 把它带回来,我们应该做生意了。
-
@ABach 为我提供的 plist 文件添加了我想要的输出