相互检查 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>
一些问题:
- 这两个文档都没有名为
cell
的*元素——您必须使用相对表达式而不是绝对表达式:
- 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()"/>