Reputation: 89
I have two xml files, I want to create a new XMl file using the first and second Xml file. I want to modify the <w>
node using second file node <entite_nom >
.
First file : [EDIT]
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transformation.xsl" ?>
<TEI xmlns="http://www.tei-c.org/ns/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 file:document.xsd">
<text>
<body>
<p>
<s>
<name>
<w type="NAM">Cussac</w>
</name>
<w lemma="en" type="PRP">en</w>
<name>
<w type="NAM">Haute-Vienne</w>
</name>
</s>
<s>
<phr type="verb_phrase" subtype="motion" function="reach">
<w lemma="prendre" type="VER:pres">prenez</w>
<w lemma="le" type="DET:ART">la</w>
<w lemma="route" type="NOM">route</w>
<offset type="temporal" subtype="final">
<w lemma="pour" type="PRP">pour</w>
<w lemma="finir" type="VER:infi">finir</w>
</offset>
<w lemma="de" type="PRP">d'</w>
<w lemma="arriver" type="VER:infi">arriver</w>
<offset type="inclusion">
<w lemma="sur" type="PRP">sur</w>
</offset>
<name>
<w type="NAM">Cussac</w>
</name>
</phr>
<pc force="strong">.</pc>
</s>
<s>
<w lemma="il" type="PRO:PER">Il</w>
<phr type="verb_phrase" subtype="position">
<w lemma="revenir" type="VER:pres">revient</w>
<w lemma="tranquillement" type="ADV">tranquillement</w>
<offset type="inclusion">
<w lemma="sur" type="PRP">sur</w>
</offset>
<name>
<w type="NAM">Cussac</w>
</name>
</phr>
<pc force="strong">.</pc>
</s>
I need to pick the Id
and value of <entite_nom>
node and add to <w>
node of the first XMl
Second file ($doc2
):
<?xml version="1.0" encoding="UTF-8"?>
<geolocalisation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="localisation-2.xsd">
<entite_nom id="en.0" lng="0.8519611512411495" lat="45.7055740420633" alt="349.0322265625">Cussac</entite_nom>
<entite_nom id="en.1" lng="0.859333" lat="45.712939" alt="280.5089111328125">Monnerie</entite_nom>
<entite_nom id="en.2" lng="0.857138" lat="45.687754" alt="376.1028137207031">Liades</entite_nom>
<entite_nom id="en.3" lng="0.8256439978387133" lat="45.67243279742165" alt="415.4050903320312">Boubon</entite_nom>
<entite_nom id="en.4" lng="0.8219117961938217" lat="45.67606269511061" alt="420.0135498046875">Villajou</entite_nom>
<entite_nom id="en.5" lng="0.8150905847864954" lat="45.68782102152895" alt="380.9435729980469">Puymoroux</entite_nom>
<entite_nom id="en.6" lng="0.8519611512411495" lat="45.7055740420633" alt="349.0322265625">Cussac</entite_nom>
<entite_nom id="en.7" lng="0.8519611512411495" lat="45.7055740420633" alt="349.0322265625">Cussac</entite_nom>
</geolocalisation>
The result I'm trying to get :
<placeName id="en.0"
ref="file:fr_Cussac_coord_entites.xml">
<name>
<w type="NPr">Cussac</w>
</name>
</placeName>
What I tried so far but i can't get the id value :
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:variable name="path">file:fr_Cussac-cood-entites.xml</xsl:variable>
<xsl:key name="ref" match="entite_nom" use="."/>
<xsl:param name="doc2" select="document('fr_Cussac-coord-entites.xml')/geolocalisation"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="tei:name[key('ref', tei:w[@type], $doc2)]">
<placeName id="{key('ref', w[@type], $doc2)/@id}" ref="{$path}">
<xsl:copy-of select="."/>
</placeName>
</xsl:template>
</xsl:stylesheet>
What i get is an empty id, I can't get the id value :
<placeName id="" ref="file:fr_Cussac-cood-entites.xml">
<name>
<w type="NAM">Cussac</w>
</name>
</placeName>
Upvotes: 1
Views: 170
Reputation: 167716
It seems a job for a key:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="#all"
version="3.0">
<xsl:key name="ref" match="entite_nom" use="."/>
<xsl:param name="doc2" select="doc('file2.xml')"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="tei:name[key('ref', tei:w[@type], $doc2)]">
<placeName id="{key('ref', tei:w[@type], $doc2)/@id}">
<xsl:copy-of select="."/>
</placeName>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/a9GPfD/1
The use of the key
function with a third argument to provide a context document or subtree is restricted to XSLT 2 and later.
If you expect to have several elements with the same word and want to find different ids based on the position in the document then
<xsl:template match="tei:name[key('ref', tei:w[@type], $doc2)]">
<xsl:variable name="word" select="tei:w[@type]"/>
<xsl:variable name="pos" as="xs:integer">
<xsl:number level="any" count="tei:name[tei:w[@type] = $word]"/>
</xsl:variable>
<placeName id="{key('ref', tei:w[@type], $doc2)[$pos]/@id}">
<xsl:copy-of select="."/>
</placeName>
</xsl:template>
https://xsltfiddle.liberty-development.net/a9GPfD/2
In XSLT 3 it might also be possible to use a composite key or an accumulator to distinguish based on position, I am currently not sure which approach is better, the above is mainly an attempt to adjust the previous suggestion to your changed, more complex requirements.
Upvotes: 1