xml - XML 根据匹配将除 text() 之外的所有内容从一个 XML 复制到另一个 XML
问题描述
您好——我是 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>
有任何想法吗?
解决方案
我会使用一个键,在 XSLT 2/3 中,您可以使用函数的第三个参数key
、模式和隧道参数优雅地做到这一点:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="lookup-doc" select="document('lookup.xml')"/>
<xsl:key name="ref" match="*[@lemma]" use="@lemma"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:mode name="ref-copy" on-no-match="shallow-copy"/>
<xsl:template match="*[key('ref', @lemma, $lookup-doc)]">
<xsl:apply-templates select="key('ref', @lemma, $lookup-doc)" mode="ref-copy">
<xsl:with-param name="text" select="string()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="text()" mode="ref-copy">
<xsl:param name="text" tunnel="yes"/>
<xsl:value-of select="$text"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/3NSSEuR
使用 XSLT 1,您需要说明两种模式的恒等式转换,并让第二种模式掌握参数,而不是依赖隧道参数。您还需要从模板内部检查查找是否存在,使用for-each select="$lookup-doc"
. 哦,你必须为命名空间声明一个前缀:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="exsl msxml"
version="1.0">
<xsl:param name="lookup-doc" select="document('lookup.xml')"/>
<xsl:key name="ref" match="tei:*[@lemma]" use="@lemma"/>
<xsl:template match="@* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@* | node()" mode="ref-copy">
<xsl:param name="text"/>
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="ref-copy">
<xsl:with-param name="text" select="$text"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="tei:*[@lemma]">
<xsl:variable name="this" select="."/>
<xsl:for-each select="$lookup-doc">
<xsl:variable name="ref-el" select="key('ref', $this/@lemma)"/>
<xsl:choose>
<xsl:when test="$ref-el">
<xsl:apply-templates select="$ref-el" mode="ref-copy">
<xsl:with-param name="text" select="string($this)"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$this">
<xsl:call-template name="identity"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()" mode="ref-copy">
<xsl:param name="text"/>
<xsl:value-of select="$text"/>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- javascript - 提交后jquery重复ajax请求
- javascript - 在打字稿中扩展方法
- amazon-web-services - ELB 健康检查失败
- php - Laravel 6 如何修复错误:即使我调用分页方法,方法 Illuminate\Database\Eloquent\Collection::links 也不存在?
- python - Django rest框架嵌套序列化程序创建方法
- c# - msbuild 失败,缺少类型或命名空间名称
- jenkins - GCE 上的 Jenkins - 将 Google Cloud Function 部署到不同的 GCP 项目
- voice - sox.core.SoxError: Stdout: Stderr: sox FAIL formats: 'XXX': WAVE: RIFF header not found when processing voice files from wxrobot
- android - 使用位图将图像文件转换为字节数组
- node.js - 反应应用程序未在具有不同节点和 npm 的计算机上启动