使用XSLT转换嵌套的XML

夏普

我似乎在XSLT方面取得了一些进展,但是我仍然很难转换嵌套的XML。XML如下所示:

<transcription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://www.livesandletters.ac.uk/schema/aor2_18112016.xsd">
    
    <page filename="00000005.tif" reader="Harvey" pagination="title page"/>
    <annotation>
        
        <marginalia hand="Italian">
            <language ident="LA">
                <position place="head" book_orientation="0">
                    <marginalia_text>gratum opus agricolis.</marginalia_text>
                </position>
            </language>
            <translation>The pleasant work for the husbandman.</translation>
        </marginalia>
        
    </annotation>
</transcription>

通过保留一些元素和属性并修改其他元素和属性,我想将其稍微转换为:

<transcription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://www.livesandletters.ac.uk/schema/aor2_18112016.xsd">
    
    <page filename="00000005.tif" reader="Harvey" pagination="title page"/>
    <annotation>
        
        <addition>
          <hand hands="Italian">
            <language ident="LA">
                <position place="head" book_orientation="0">
                    <note>gratum opus agricolis.</note>
                </position>
            </language>
            <translation>The pleasant work for the husbandman.</translation>
        </addition>
        
    </annotation>
</transcription>

到目前为止,我已经生成了该XSL,但是由于某种原因,它不会复制position元素的属性或marginalia_text元素的内容(在转换后的XML文件中成为note元素)。知道我在做什么错吗?我考虑过要提到各种模板,但这也不起作用...

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        
    <xsl:template match="marginalia">
        <additions>
            <xsl:element name="hand">
                <xsl:attribute name="hands"><xsl:value-of select="@hand"/></xsl:attribute>
                <xsl:element name="position">
                    <xsl:attribute name="place"><xsl:value-of select="@place" /></xsl:attribute>
                    <xsl:attribute name="book_orientation"><xsl:value-of select="@book_orientation" /> 
                    </xsl:attribute>
                    <xsl:element name="note"><xsl:value-of select="marginalia_text"/>
                    </xsl:element>
                </xsl:element> 
            </xsl:element>
            <xsl:element name="translation"><xsl:value-of select="translation"/></xsl:element>
        </additions>
    </xsl:template>
</xsl:stylesheet>
伊扎克·哈宾斯基

您只需要使用所谓的“身份转换”模式。总体而言,您需要两个专用模板来处理marginaliamarginalia_text元素。其他所有内容将从源XML文档照原样复制。

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>

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

    <xsl:template match="marginalia">
        <addition>
            <hand hands="{@hand}"/>
            <xsl:apply-templates/>
        </addition>
    </xsl:template>

    <xsl:template match="marginalia_text">
        <note>
            <xsl:value-of select="."/>
        </note>
    </xsl:template>
</xsl:stylesheet>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章