我有这个xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dev>
<word string="test1" occurrences="39">
<systems>
<system name="paraview">
<pos file="" part="noun" line="0" occ="39"/>
</system>
</systems>
</word>
<word string="Format" occurrences="39">
<systems>
<system name="paraview">
<pos file="" part="verb" line="0" occ="40"/>
</system>
</systems>
</word>
<word string="Source" occurrences="16">
<systems>
<system name="paraview">
<pos file="" part="verb" line="0" occ="16"/>
</system>
</systems>
</word>
<word string="p4" occurrences="1">
<systems>
<system name="Pstoedit">
<pos file="" part="pronoun" line="0" occ="1"/>
</system>
</systems>
</word>
<word string="test1" occurrences="3">
<systems>
<system name="Pstoedit">
<pos file="" part="noun" line="0" occ="3"/>
</system>
</systems>
</word>
</dev>
我试图提取指定以下条件的这两个系统[paraview&Pstoedit],这两个系统应该具有相同的确切字符串。所以输出应该给我匹配的数量,然后说是的,有两个系统要满足您的条件,匹配的字符串是test1
我使用xmllint尝试了不同的命令,而我认为最接近的最终命令是:
xmllint --xpath '//word/systems/system[@name="Pstoedit"][@name="paraview"]/pos[@part="noun"]' file.xml
如何在xpath中使用两个条件进行比较?请指教。
更新:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="paraview" match="system[@name='paraview']" use="ancestor::word/@string" />
<xsl:template match="/dev">
<html>
<body>
<h2>Number of matched words</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Total</th>
<th>Words</th>
</tr>
<xsl:variable name="pairs" select="word/systems/system[@name='Pstoedit'][key('paraview', ancestor::word/@string)]" />
<result>
<tr>
<count-pairs>
<td> <xsl:value-of select="count($pairs)"/> </td>
</count-pairs>
</tr>
<tr>
<match-strings>
<td> <xsl:for-each select="$pairs"> </td>
<string>
<td> <xsl:value-of select="ancestor::word/@string"/> </td>
</string>
</tr>
</xsl:for-each>
</match-strings>
</result>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这是我得到的结果:结果我需要将它们安排在基于以前的xls和html代码的表中,例如:
我试图提取指定以下条件的这两个系统[paraview&Pstoedit],这两个系统应该具有相同的确切字符串。
您可以通过以下方式选择所有具有匹配系统的Pstoedit
系统(或更确切地说,其祖先word
)paraview
:
/dev/word[systems/system/@name='Pstoedit'][@string=/dev/word[systems/system/@name='paraview']/@string]
输出应该给我匹配的数量,然后说是的,有两个系统要满足您的条件...
好吧,每个选定的节点都代表一对,因此:
2 * count ( $theabovexpression )
会给您涉及的节点总数。
...并且匹配的字符串是test1
我不认为仅XPath就有办法做到这一点。请记住,可能有不止一对,并且每个匹配的字符串可能都不同。但是,在XSLT中,您可以执行以下操作:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="paraview" match="system[@name='paraview']" use="ancestor::word/@string" />
<xsl:template match="/dev">
<xsl:variable name="pairs" select="word/systems/system[@name='Pstoedit'][key('paraview', ancestor::word/@string)]" />
<result>
<count-pairs>
<xsl:value-of select="count($pairs)"/>
</count-pairs>
<match-strings>
<xsl:for-each select="$pairs">
<string>
<xsl:value-of select="ancestor::word/@string"/>
</string>
</xsl:for-each>
</match-strings>
</result>
</xsl:template>
</xsl:stylesheet>
返回:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<count-pairs>1</count-pairs>
<match-strings>
<string>test1</string>
</match-strings>
</result>
我只想要两列表中的字符串列表和总计数
尝试这种方式:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="paraview" match="system[@name='paraview']" use="ancestor::word/@string" />
<xsl:template match="/dev">
<xsl:variable name="pairs" select="word/systems/system[@name='Pstoedit'][key('paraview', ancestor::word/@string)]" />
<xsl:variable name="count-pairs" select="count($pairs)" />
<table border="1">
<tr>
<th>Strings</th>
<th>Total</th>
</tr>
<xsl:for-each select="$pairs">
<tr>
<td><xsl:value-of select="ancestor::word/@string"/></td>
<xsl:if test="position() = 1">
<td rowspan="{$count-pairs}">
<xsl:value-of select="$count-pairs"/>
</td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
这将返回(例如,当找到两个匹配对时):
<table border="1">
<tr>
<th>Strings</th>
<th>Total</th>
</tr>
<tr>
<td>stringone</td>
<td rowspan="2">2</td>
</tr>
<tr>
<td>stringtwo</td>
</tr>
</table>
您的浏览器应将其呈现为:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句