首页 > 解决方案 > XML 根据匹配将除 text() 之外的所有内容从一个 XML 复制到另一个 XML

问题描述

您好——我是 XSLT 的新手:

我有两个带有一些匹配元素的 XML TEI 文件。我想用第二个文件中TEI_test.xml的相应<w>元素替换第一个文件中的每个元素lookup.xml,以便@lemma第一个文件中的属性与第二个文件中的属性匹配@lemmalookup.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>

有任何想法吗?

标签: xmlxslttei

解决方案


我会使用一个键,在 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>

https://xsltfiddle.liberty-development.net/3NSSEuR/1


推荐阅读