I am trying to insert values into an attribute based on matching an id from one XML document to a lookup XML document and substituting in the value contained in the matching node. I can make and evaluate the match correctly, but I can't figure out how to insert the value of the matched node into the resulting XML. In this specifically, I am trying to take the value of our current word number, lookup the matching id in our list of values matched to our old numbering system, and insert that value as an attribute in the combined XML document output. If there isn't a match, it will just put the current NO field there.
lookup.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<mdict>
<PR id="NEW1">OLD1</PR>
<PR id="NEW2">OLD2</PR>
<PR id="NEW3">OLD3</PR>
<PR id="NEW4">OLD4</PR>
<PR id="NEW5">OLD5</PR>
<PR id="NEW6">OLD6</PR>
</mdict>
XML:
<?xml version = "1.0" encoding = "UTF-8"?>
<dictionary>
<entry><NO>NEW1</NO><PR>phon text1</PR></entry>
<entry><NO>NEW2</NO><PR>phon text2</PR></entry>
<entry><NO>NEW3</NO><PR>phon text3</PR></entry>
<entry><NO>NEW4</NO><PR>phon text4</PR></entry>
<entry><NO>NEW5</NO><PR>phon text5</PR></entry>
<entry><NO>NEW6</NO><PR>phon text6</PR></entry>
</dictionary>
XSLT:
?xml version="1.0" encoding="UTF-8"?>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:variable name='prons' select='document("lookup.xml")'/>
<xsl:key name='pron' match='PR' use='@id' />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="PR">
<xsl:param name="curr-pron"/>
<xsl:choose>
<xsl:when test="../preceding-sibling::NO/text() = $prons/mdict/PR/@id">
<xsl:value-of select="key('pron', $curr-pron)/PR"/>
<pr pron="{$curr-pron}"><xsl:apply-templates/></pr>
</xsl:when>
<xsl:otherwise>
<pr pron="{../preceding-sibling::NO}"><xsl:apply-templates/></pr>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Desired output:
<dictionary>
<entry><NO>NEW1</NO><pr pron="OLD1">phon text1</pr></entry>
<entry><NO>NEW2</NO><pr pron="OLD2">phon text2</pr></entry>
<entry><NO>NEW3</NO><pr pron="OLD3">phon text3</pr></entry>
<entry><NO>NEW4</NO><pr pron="OLD4">phon text4</pr></entry>
<entry><NO>NEW5</NO><pr pron="OLD5">phon text5</pr></entry>
<entry><NO>NEW6</NO><pr pron="OLD6">phon text6</pr></entry>
</dictionary>
Thanks!
I'd suggest you try it this way:
XSLT 1.0
<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:strip-space elements="*"/>
<xsl:key name="old-pron" match="PR" use="@id" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PR">
<xsl:variable name="id" select="../NO" />
<pr>
<xsl:attribute name="pron">
<!-- switch context to use key -->
<xsl:for-each select="document('lookup.xml')">
<xsl:value-of select="key('old-pron', $id)"/>
</xsl:for-each>
</xsl:attribute>
<xsl:apply-templates/>
</pr>
</xsl:template>
</xsl:stylesheet>
This assumes you are processing the XML document above.
Note that in XSLT 1.0, keys cannot work across documents.