XSLT:复制部分外部 XML 并更改指定元素

问题描述

这个问题看起来很基本,但我仍然不知道如何只用一个样式表来完成这个简单的任务:

从外部 XML 复制一部分并在输出中更改该部分中的一个特定元素:

    <xsl:template match="@*|node()">
        <xsl:copy> 
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!--This part loops,i.e. does not work: copy-of works but then I cannot change anything of course -->
    <xsl:template match="t:teiHeader">
         <xsl:apply-templates select="document('example.xml')//t:teiHeader"/>
    </xsl:template>
    
    <xsl:template match="t:publicationStmt/t:ref[@target='bla1']">
        <ref target="bla2"/>
    </xsl:template>

我唯一的想法是使用两个样式表。先copy-of,然后更改所需的元素。但我想这不可能那么难。我做错了什么?

解决方法

我认为您想使用两种模式,以便您可以有两种处理 t:teiHeader 元素的方式,例如

<xsl:template match="t:teiHeader">
   <xsl:apply-templates select="document('example.xml')//t:teiHeader" mode="copy"/>
</xsl:template>

加上

<xsl:template match="t:teiHeader" mode="copy">
   <xsl:copy>
      <xsl:apply-templates select="@* | node()" mode="#default"/>
   </xsl:copy>
</xsl:template>

或加号

<xsl:mode name="copy" on-no-match="shallow-copy"/> 

<xsl:template mode="copy" match="t:publicationStmt/t:ref[@target='bla1']"><ref target="bla2"/></xsl:template>
,

这也有效(不需要模式),虽然我不确定为什么(也许有人可以解释):

<xsl:template match="t:teiHeader">
         <xsl:apply-templates select="document('example.xml')//t:teiHeader/(@*|node())"/>
 </xsl:template>