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>
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>
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>
Building on Dimitre's answer, you can abstract this "look up an IDREF" pattern out into a separate template using
key()
(for fast lookups) andcall-template
:id-lookup.xsl
replace-id-with-value.xsl
When you run this through an xslt processor, you get the document with all
<id>
elements replaced with<val>
and the corresponding value.Using
xsltproc
, you would run this like so:You can even change the lookup document using an xslt param:
Whatever XSLT processor you are using will have some way of specifying a template param.
A number of problems:
cell
-- instead of absolute expression you must use a relative one:.....
and
because a document node doesn't have siblings.
.2. The document that has element named
cell
doesn't have elements namedid
. 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:Finally, all this can be written shortly as: