您好——我是 XSLT 的新手:
我有两个带有一些匹配元素的 XML TEI 文件。我想用第二个文件中TEI_test.xml
的相应<w>
元素替换第一个文件中的每个元素lookup.xml
,以便@lemma
第一个文件中的属性与第二个文件中的属性匹配@lemma
(lookup.xml
)。除了实际的text()
. 如果没有匹配,TEI_test.xml
则应保留原始元素。
这是TEI_test.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader><fileDesc>
<titleStmt>
<title></title>
</titleStmt>
<publicationStmt><publisher></publisher></publicationStmt>
<sourceDesc><p></p></sourceDesc>
</fileDesc>
</teiHeader><text><body>
<p xml:lang="arn" n="3">
<w xml:lang="" lemma="ta">ta</w>
<w xml:lang="" lemma="ella">ella</w>
<w xml:lang="" lemma="rüpü">rùpù</w>
<w xml:lang="" lemma="rüpüwe">rùpùwe</w>
</p>
</body>
</text>
</TEI>
这是查找表:lookup.xml
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader><fileDesc>
<titleStmt>
<title></title>
</titleStmt>
<publicationStmt><publisher></publisher></publicationStmt>
<sourceDesc><p></p></sourceDesc>
</fileDesc>
</teiHeader><text><body><p>
<w xml:lang="arn" lemma="mew" pos="P"><m baseForm="mew" type="root" corresp="P">meu</m></w>
<w xml:lang="arn" lemma="ta" pos="DA"><m baseForm="ta" type="root" corresp="DA">ta</m></w>
<w xml:lang="arn" lemma="rüpü" pos="N" corresp="path/road"><m baseForm="rüpü" type="root" corresp="path/road">rüpü</m></w>
<w xml:lang="arn" lemma="rüpüwe" pos="N" corresp="place of path/road"><m baseForm="rüpü" type="root" corresp="path/road">rüpü</m><m baseForm="we" type="instrumental">we</m></w>
</p>
</body></text></TEI>
我想出的XSLT如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:tei="http://www.tei-c.org/ns/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:param name="path-to-lookup" select="'lookup1.xml'" />
<xsl:param name="path-to-orig" select="'TEI_test.xml.xml'" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tei:w">
<xsl:choose>
<xsl:when test="@lemma =document($path-to-lookup)//tei:w[@lemma]">
<xsl:copy-of select="document($path-to-lookup)//tei:w[@lemma=current()/@lemma]">
</xsl:copy-of>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise></xsl:choose>
<xsl:choose>
<xsl:when test="@lemma =document($path-to-lookup)//tei:w[@lemma]">
<xsl:copy-of select="document($path-to-orig)//tei:w[text()=current()/text()]"/>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise></xsl:choose>
</xsl:template>
</xsl:stylesheet>`
虽然这设法复制了所有匹配的<w>
节点,但w[@lemma]
在保存 时它不会产生预期的结果w/text()
,这将在第二个<xsl:choose>
系列中解决。这是我得到的:
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title/>
</titleStmt>
<publicationStmt>
<publisher/>
</publicationStmt>
<sourceDesc>
<p/>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<body>
<p xml:lang="arn" n="3">
<w xml:lang="arn" lemma="ta" pos="DA">
<m baseForm="ta" type="root" corresp="DA">ta</m>
</w>
<w xml:lang="" lemma="ta">ta</w>
<w xml:lang="" lemma="ella">ella</w>
<w xml:lang="arn" lemma="rüpü" pos="N" corresp="path/road">
<m baseForm="rüpü" type="root" corresp="path/road">rüpü</m>
</w>
<w xml:lang="" lemma="rüpü">rùpù</w>
<w xml:lang="arn" lemma="rüpüwe" pos="N" corresp="place of path/road">
<m baseForm="rüpü" type="root" corresp="path/road">rüpü</m>
<m baseForm="we" type="instrumental">we</m>
</w>
</p>
</body>
</text>
</TEI>
我真正想要获得的是:
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title/>
</titleStmt>
<publicationStmt>
<publisher/>
</publicationStmt>
<sourceDesc>
<p/>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<body>
<p xml:lang="arn" n="3">
<w xml:lang="arn" lemma="ta" pos="DA">
<m baseForm="ta" type="root" corresp="DA">ta</m>
</w>
<w xml:lang="" lemma="ella">ella</w>
<w xml:lang="arn" lemma="rüpü" pos="N" corresp="path/road">
<m baseForm="rüpü" type="root" corresp="path/road">rùpù</m>
</w>
<w xml:lang="arn"
lemma="rüpüwe" pos="N" corresp="place of path/road">
<m baseForm="rüpü" type="root" corresp="path/road">rùpùwe</m>
<m baseForm="we" type="instrumental">rùpùwe</m>
</w>
</p>
</body>
</text>
</TEI>
有任何想法吗?