XSLT:比较两个文件和列表

时间:2016-10-12 14:33:54

标签: xml xslt xpath xslt-1.0

我正在尝试比较两个xml文件:

$ cat input.xml
<rootnode>
 <section id="1" >
  <outer param="p1" >
   <inner />
   <inner />
  </outer>
  <outer >
   <inner />
  </outer>
  <outer />
  <outer />
 </section>
 <section id="2" >
  <outer >
   <inner />
   <inner />
   <inner />
  </outer>
 </section>
 <section id="3" >
  <outer >
   <inner />
  </outer>
 </section>
 <section id="7" >
  <outer >
   <inner />
  </outer>
 </section>
</rootnode>

另一个文件是:

$ cat result.xml 
<rootnode>
 <section id="1" status="fail">
  <outer param="p1" status="fail">
   <inner status="fail"/>
   <inner status="pass"/>
  </outer>
  <outer status="pass">
   <inner status="pass"/>
  </outer>
  <outer status="pass"/>
  <outer status="fail"/>
 </section>
 <section id="2" status="fail">
  <outer status="fail">
   <inner status="pass"/>
   <inner status="fail"/>
   <inner status="inc"/>
  </outer>
 </section>
 <section id="5" status="pass">
  <outer status="pass">
   <inner status="pass"/>
  </outer>
 </section>
 <section id="6" status="inc">
  <outer status="inc">
   <inner status="inc"/>
  </outer>
 </section>
</rootnode>

我想打印<section>中不属于input.xml的{​​{1}}个节点。可以通过result.xml属性唯一标识节点。 我试过这个XSLT文件:

id

但是,这只返回id值。我需要整个节节点。 我只是在$ cat missing.xsl <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:param name="doc" select="document('result.xml')"/> <xsl:template match="/"> <xsl:variable name="var" select="$doc/rootnode/section" /> <root> <xsl:for-each select="//section/@id[not(. = $var/@id)] "> <missing><xsl:value-of select="."/></missing> </xsl:for-each> </root> </xsl:template> </xsl:stylesheet> 中使用了错误的xpath,还是我的方法存在根本缺陷?

PS:解决方案应该在XSLT 1.0中。

1 个答案:

答案 0 :(得分:1)

您目前正在选择@id属性,而不是section元素本身,因此xsl:value-of只会获取该属性的值。

只需将select表达式更改为选择section元素,然后将@id移至条件

    <xsl:for-each select="//section[not(@id = $var/@id)] ">
        <missing><xsl:copy-of select="."/></missing>
    </xsl:for-each>

请注意使用xsl:copy-of复制section元素。 xsl:value-of用于获取节点的字符串值,而不是节点本身。

相关问题