xml - XSLT-如何根据来自另一个 XML 的信息插入标签属性
问题描述
我有一个名为“fr_Cussac.xm”的 XML 文件,如下所示:
<TEI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 file:fr_Cussac.xsd"
xmlns="http://www.tei-c.org/ns/1.0"><TEI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 file:fr_Cussac.xsd"
xmlns="http://www.tei-c.org/ns/1.0">
<text>
<body>
<p>
<s>
<w lemma="boucle" type="NOM">Boucle</w>
<w lemma="de" type="PRP">de</w>
<w lemma="randonner" type="VER:pres">randonnée</w>
<w lemma="pouvoir" type="VER:ppre">pouvant</w>
<w lemma="se" type="PRO:PER">se</w>
<w lemma="faire" type="VER:infi">faire</w>
<w lemma="à" type="PRP">à</w>
<w lemma="pied" type="NOM">pieds</w>
<w lemma="ou" type="KON">ou</w>
<w lemma="à" type="PRP">à</w>
<w lemma="unknown" type="NAM">VTT</w>
<w lemma="." type="SENT">.</w>
</s>
<s>
<w lemma="elle" type="PRO:PER">Elle</w>
<phr type="motion">
<w lemma="partir" type="VER:pres">part</w>
<w lemma="de" type="PRP">de</w>
<w lemma="unknown" type="NAM">Cussac</w>
<offset type="inclusion">
<w lemma="en" type="PRP">en</w>
</offset>
<w lemma="unknown" type="NAM">Haute-Vienne</w>
<w lemma="," type="PUN">,</w>
<w lemma="sur" type="PRP">sur</w>
<term type="N">
<w lemma="le" type="DET:ART">la</w>
<w lemma="place" type="NOM">place</w>
</term>
<offset type="direction" subtype="initial">
<w lemma="de" type="PRP">de</w>
</offset>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
</phr>
<w lemma="puis" type="ADV">puis</w>
<phr type="motion">
<w lemma="descendre" type="VER:pres">descend</w>
<w lemma="vers" type="PRP">vers</w>
<w lemma="le" type="DET:ART">la</w>
<w lemma="station" type="NOM">station</w>
<w lemma="de" type="PRP">d'</w>
<geogFeat>
<w lemma="épuration" type="NOM">épuration</w>
</geogFeat>
</phr>
<phr type="motion">
<w lemma="emprunter" type="VER:pres">empruntant</w>
<geogName type="R" subtype="TRL">
<geogFeat>
<w lemma="le" type="DET:ART">le</w>
<w lemma="sentier" type="NOM">sentier</w>
<w lemma="du" type="PREPDET">du</w>
<w lemma="parcours" type="NOM">parcours</w>
<w lemma="sportif" type="A">sportif</w>
</geogFeat>
</geogName>
</phr>
<w lemma="derrière" type="PRP">derrière</w>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
<w lemma="." type="SENT">.</w>
</s>
<text>
<body>
<p>
<s>
<w lemma="boucle" type="NOM">Boucle</w>
<w lemma="de" type="PRP">de</w>
<w lemma="randonner" type="VER:pres">randonnée</w>
<w lemma="pouvoir" type="VER:ppre">pouvant</w>
<w lemma="se" type="PRO:PER">se</w>
<w lemma="faire" type="VER:infi">faire</w>
<w lemma="à" type="PRP">à</w>
<w lemma="pied" type="NOM">pieds</w>
<w lemma="ou" type="KON">ou</w>
<w lemma="à" type="PRP">à</w>
<w lemma="unknown" type="NAM">VTT</w>
<w lemma="." type="SENT">.</w>
</s>
<s>
<w lemma="elle" type="PRO:PER">Elle</w>
<phr type="motion">
<w lemma="partir" type="VER:pres">part</w>
<w lemma="de" type="PRP">de</w>
<w lemma="unknown" type="NAM">Cussac</w>
<offset type="inclusion">
<w lemma="en" type="PRP">en</w>
</offset>
<w lemma="unknown" type="NAM">Haute-Vienne</w>
<w lemma="," type="PUN">,</w>
<w lemma="sur" type="PRP">sur</w>
<term type="N">
<w lemma="le" type="DET:ART">la</w>
<w lemma="place" type="NOM">place</w>
</term>
<offset type="direction" subtype="initial">
<w lemma="de" type="PRP">de</w>
</offset>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
</phr>
<w lemma="puis" type="ADV">puis</w>
<phr type="motion">
<w lemma="descendre" type="VER:pres">descend</w>
<w lemma="vers" type="PRP">vers</w>
<w lemma="le" type="DET:ART">la</w>
<w lemma="station" type="NOM">station</w>
<w lemma="de" type="PRP">d'</w>
<geogFeat>
<w lemma="épuration" type="NOM">épuration</w>
</geogFeat>
</phr>
<phr type="motion">
<w lemma="emprunter" type="VER:pres">empruntant</w>
<geogName type="R" subtype="TRL">
<geogFeat>
<w lemma="le" type="DET:ART">le</w>
<w lemma="sentier" type="NOM">sentier</w>
<w lemma="du" type="PREPDET">du</w>
<w lemma="parcours" type="NOM">parcours</w>
<w lemma="sportif" type="A">sportif</w>
</geogFeat>
</geogName>
</phr>
<w lemma="derrière" type="PRP">derrière</w>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
<w lemma="." type="SENT">.</w>
<w type="NAM">Boubon</w>
</s>
</p>
</body>
</text>
</TEI>
和另一个名为“fr_Cussac-coord-entites.xml”的文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<geolocalisation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="localisation-2.xsd">
<entite_spatiale id="ene.0" lng="0.8519611512411495" lat="45.7055740420633" alt="349.0322265625">Cussac</entite_spatiale>
<entite_spatiale id="ene.1" lng="0.859333" lat="45.712939" alt="280.5089111328125">Monnerie</entite_spatiale>
<entite_spatiale id="ene.2" lng="0.857138" lat="45.687754" alt="376.1028137207031">Liades</entite_spatiale>
<entite_spatiale id="ene.3" lng="0.8256439978387133" lat="45.67243279742165" alt="415.4050903320312">Boubon</entite_spatiale>
<entite_spatiale id="ene.4" lng="0.8219117961938217" lat="45.67606269511061" alt="420.0135498046875">Villajou</entite_spatiale>
<entite_spatiale id="ene.5" lng="0.8150905847864954" lat="45.68782102152895" alt="380.9435729980469">Puymoroux</entite_spatiale>
<entite_spatiale id="ene.6" lng="0.8519611512411495" lat="45.7055740420633" alt="349.0322265625">Cussac</entite_spatiale>
<entite_spatiale id="ene.7" lng="0.8519611512411495" lat="45.7055740420633" alt="349.0322265625">Cussac</entite_spatiale>
</geolocalisation>
我想遍历第一个文件,并且对于每个“type”属性等于“NAM”的 w 标签,在第二个文件中找到一个匹配项,并用具有相应 id 的 placeName 标签包围它。(例如:匹配<w>Cussac</w> with <entite_spatial>Cussac</entite_spatial>
并获取 id)
预期输出:
<TEI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 file:fr_Cussac.xsd"
xmlns="http://www.tei-c.org/ns/1.0"><TEI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 file:fr_Cussac.xsd"
xmlns="http://www.tei-c.org/ns/1.0">
<text>
<body>
<p>
<s>
<w lemma="boucle" type="NOM">Boucle</w>
<w lemma="de" type="PRP">de</w>
<w lemma="randonner" type="VER:pres">randonnée</w>
<w lemma="pouvoir" type="VER:ppre">pouvant</w>
<w lemma="se" type="PRO:PER">se</w>
<w lemma="faire" type="VER:infi">faire</w>
<w lemma="à" type="PRP">à</w>
<w lemma="pied" type="NOM">pieds</w>
<w lemma="ou" type="KON">ou</w>
<w lemma="à" type="PRP">à</w>
<w lemma="unknown" type="NAM">VTT</w>
<w lemma="." type="SENT">.</w>
</s>
<s>
<w lemma="elle" type="PRO:PER">Elle</w>
<phr type="motion">
<w lemma="partir" type="VER:pres">part</w>
<w lemma="de" type="PRP">de</w>
<placeName id="ene.0"><!-- EXAMPLE HERE-->
<w lemma="unknown" type="NAM">Cussac</w>
</placeName>
<offset type="inclusion">
<w lemma="en" type="PRP">en</w>
</offset>
<w lemma="unknown" type="NAM">Haute-Vienne</w>
<w lemma="," type="PUN">,</w>
<w lemma="sur" type="PRP">sur</w>
<term type="N">
<w lemma="le" type="DET:ART">la</w>
<w lemma="place" type="NOM">place</w>
</term>
<offset type="direction" subtype="initial">
<w lemma="de" type="PRP">de</w>
</offset>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
</phr>
<w lemma="puis" type="ADV">puis</w>
<phr type="motion">
<w lemma="descendre" type="VER:pres">descend</w>
<w lemma="vers" type="PRP">vers</w>
<w lemma="le" type="DET:ART">la</w>
<w lemma="station" type="NOM">station</w>
<w lemma="de" type="PRP">d'</w>
<geogFeat>
<w lemma="épuration" type="NOM">épuration</w>
</geogFeat>
</phr>
<phr type="motion">
<w lemma="emprunter" type="VER:pres">empruntant</w>
<geogName type="R" subtype="TRL">
<geogFeat>
<w lemma="le" type="DET:ART">le</w>
<w lemma="sentier" type="NOM">sentier</w>
<w lemma="du" type="PREPDET">du</w>
<w lemma="parcours" type="NOM">parcours</w>
<w lemma="sportif" type="A">sportif</w>
</geogFeat>
</geogName>
</phr>
<w lemma="derrière" type="PRP">derrière</w>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
<w lemma="." type="SENT">.</w>
</s>
<text>
<body>
<p>
<s>
<w lemma="boucle" type="NOM">Boucle</w>
<w lemma="de" type="PRP">de</w>
<w lemma="randonner" type="VER:pres">randonnée</w>
<w lemma="pouvoir" type="VER:ppre">pouvant</w>
<w lemma="se" type="PRO:PER">se</w>
<w lemma="faire" type="VER:infi">faire</w>
<w lemma="à" type="PRP">à</w>
<w lemma="pied" type="NOM">pieds</w>
<w lemma="ou" type="KON">ou</w>
<w lemma="à" type="PRP">à</w>
<w lemma="unknown" type="NAM">VTT</w>
<w lemma="." type="SENT">.</w>
</s>
<s>
<w lemma="elle" type="PRO:PER">Elle</w>
<phr type="motion">
<w lemma="partir" type="VER:pres">part</w>
<w lemma="de" type="PRP">de</w>
<placeName id="ene.0"><!--EXAMPLE HERE-->
<w lemma="unknown" type="NAM">Cussac</w>
</placeName>
<offset type="inclusion">
<w lemma="en" type="PRP">en</w>
</offset>
<w lemma="unknown" type="NAM">Haute-Vienne</w>
<w lemma="," type="PUN">,</w>
<w lemma="sur" type="PRP">sur</w>
<term type="N">
<w lemma="le" type="DET:ART">la</w>
<w lemma="place" type="NOM">place</w>
</term>
<offset type="direction" subtype="initial">
<w lemma="de" type="PRP">de</w>
</offset>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
</phr>
<w lemma="puis" type="ADV">puis</w>
<phr type="motion">
<w lemma="descendre" type="VER:pres">descend</w>
<w lemma="vers" type="PRP">vers</w>
<w lemma="le" type="DET:ART">la</w>
<w lemma="station" type="NOM">station</w>
<w lemma="de" type="PRP">d'</w>
<geogFeat>
<w lemma="épuration" type="NOM">épuration</w>
</geogFeat>
</phr>
<phr type="motion">
<w lemma="emprunter" type="VER:pres">empruntant</w>
<geogName type="R" subtype="TRL">
<geogFeat>
<w lemma="le" type="DET:ART">le</w>
<w lemma="sentier" type="NOM">sentier</w>
<w lemma="du" type="PREPDET">du</w>
<w lemma="parcours" type="NOM">parcours</w>
<w lemma="sportif" type="A">sportif</w>
</geogFeat>
</geogName>
</phr>
<w lemma="derrière" type="PRP">derrière</w>
<geogName type="S" subtype="CH">
<geogFeat>
<w lemma="le" type="DET:ART">l'</w>
<w lemma="église" type="NOM">église</w>
</geogFeat>
</geogName>
<w lemma="." type="SENT">.</w>
<placeName id="ene.5"><!-- EXAMPLE HERE-->
<w type="NAM">Boubon</w>
</placeName>
</s>
</p>
</body>
</text>
</TEI>
我有一个看起来像这样的 xslt 草稿,但我绝对不确定:
<?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"
exclude-result-prefixes="xs"
version="2.0" xmlns:tei="http://www.tei-c.org/ns/1.0">
<xsl:output method=" xml " indent=" yes "/>
<xsl:strip-space elements="tei:p tei:w tei:s tei:body tei:text"/>
<xsl:variable name="positions" select="document('fr_Cussac-coord-entites.xml')"/>
<xsl:template match="tei:TEI">
<TEI>
<xsl:apply-templates></xsl:apply-templates>
</TEI>
</xsl:template>
<xsl:template match="tei:text">
<text>
<xsl:apply-templates></xsl:apply-templates>
</text>
</xsl:template>
<xsl:template match="tei:body">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="tei:p">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="tei:s">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="tei:w[@type = 'NAM']">
<placeName>
<w>
<xsl:apply-templates/>
</w>
</placeName>
</xsl:template>
<xsl:template match="tei:w[not(@type = 'NAM')]">
<w lemma= "{@lemma}" type="{@type}">
<xsl:apply-templates/>
</w>
</xsl:template>
<xsl:template match="tei:phr">
<phr>
<xsl:apply-templates/>
</phr>
</xsl:template>
<xsl:template match="tei:geogFeat">
<geoFeat>
<xsl:apply-templates/>
</geoFeat>
</xsl:template>
</xsl:stylesheet>
我是 xslt 的新手,我不知道该怎么做,这就是我寻求您帮助的原因。感谢您的阅读以及那些将回复的人
解决方案
在 XSLT 代码中将键声明<xsl:key name="ref" match="entite_spatiale" use="."/>
为顶级元素,然后编写模板
<xsl:template match="tei:w[@type = 'NAM'][key('ref', ., $positions)]">
<placeName xmlns="http://www.tei-c.org/ns/1.0" id="{key('ref', ., $positions)/@id}>
<xsl:copy-of select="."/>
</placeName>
</xsl:template>
我将通过身份转换处理其余部分,除非您也想更改/转换其他元素。