相互检查 2 个不同 xml 文档的值 [英] Checking values of 2 different xml documents against each other

查看:25
本文介绍了相互检查 2 个不同 xml 文档的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 2 个 XML 文档,并尝试在匹配变量的情况下将一个值从一个文档获取到另一个文档中.第一个 XML 文档是一个转换后的电子表格,格式如下:

I'm working with 2 XML documents, and trying to get a value from one document into the other if a variable is matched. The first XML document is a converted spreadsheet formatted like:

<Doc1>
    <row>
      <cell>VA15</cell>
      <cell>wr23</cell>
    </row>
    <row>
      <cell>VA45</cell>
      <cell>wr27</cell>
    </row>        <row>
      <cell>VA78</cell>
      <cell>wr24</cell>
    </row>
</Doc1>

第二个 XML 文档较长,其中有一个 id 元素与电子表格的一部分匹配:

The second XML document is a longer one inside of which there's an id element matching one part of the spreadsheet:

<Doc2>
 <p> text text text
  <id>wr23</id>
 </p>
</Doc2>

我正在尝试使用我的 xslt 转换来测试 id 元素是否与 doc1 中的 cell 的值匹配,它会提取前面的 单元格.在这种情况下,我希望 xslt 转换输出VA15".我已经尝试了以下代码的各种排列但没有成功,有人有任何想法吗?

I'm trying with my xslt transformation to test to see if the id element matches the value of a cell in doc1 it pulls the value of the preceding cell. In this case I'd like the xslt transformation to output "VA15". I've tried various permutations of the following code without success, does anyone have any ideas?

<xsl:for-each select="document('Doc1.xml')//row">
   <xsl:if test="/cell=//id'">
     <xsl:value-of select="/preceding-sibling::cell"/>
   </xsl:if>
</xsl:for-each>

推荐答案

<xsl:for-each select="document('Doc1.xml')//row">
   <xsl:if test="/cell=//id'">
     <xsl:value-of select="/preceding-sibling::cell"/>
   </xsl:if>
</xsl:for-each>

一些问题:

  1. 这两个文档都没有名为 cell 的顶级元素——您必须使用相对表达式而不是绝对表达式:
  1. None of the two documents has a top element named cell -- instead of absolute expression you must use a relative one:

.....

cell = //id

  preceding-sibling::cell

因为文档节点没有兄弟节点.

because a document node doesn't have siblings.

.2.具有名为 cell 的元素的文档没有名为 id 的元素.当 XPath 表达式引用多个文档时,必须显式引用除一个文档之外的所有文档.进行所有更正后,您的代码将变成这样:

.2. The document that has element named cell doesn't have elements named id. When an XPath expression references more than one document, all documents but one must be explicitly referenced. With all corrections, your code becomes something like this:

  <xsl:variable name="vthisDoc" select="/"/>

  <xsl:for-each select="document('Doc1.xml')//row">
     <xsl:if test="cell=$vthisDoc//id'">
       <xsl:value-of select="preceding-sibling::cell"/>
     </xsl:if>
  </xsl:for-each>

最后,所有这些都可以简单地写成:

Finally, all this can be written shortly as:

<xsl:copy-of select=
 "document('Doc1.xml')//row[cell=$vthisDoc//id]/preceding-sibling::cell[1]/text()"/>

这篇关于相互检查 2 个不同 xml 文档的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆