用XSLT转换嵌套XML

用XSLT转换嵌套XML,xml,xslt,Xml,Xslt,我似乎在XSLT方面取得了一些进展,但我仍然发现转换嵌套XML非常困难。XML如下所示: <transcription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.livesandletters.ac.uk/schema/aor2_18112016.xsd"> <pa

我似乎在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_文本元素(它将成为转换后的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>

您只缺少所需属性或元素的路径。 请记住,模板与“边缘”节点匹配。从这里开始,您必须指定元素/属性的路径

<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="language/position/@place" /></xsl:attribute>
                    <xsl:attribute name="book_orientation"><xsl:value-of select="language/position/@book_orientation" /> 
                    </xsl:attribute>
                    <xsl:element name="note"><xsl:value-of select="language/position/marginalia_text"/>
                    </xsl:element>
                </xsl:element> 
            </xsl:element>
            <xsl:element name="translation"><xsl:value-of select="translation"/></xsl:element>
        </additions>
    </xsl:template>
</xsl:stylesheet>


您只缺少所需属性或元素的路径。 请记住,模板与“边缘”节点匹配。从这里开始,您必须指定元素/属性的路径

<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="language/position/@place" /></xsl:attribute>
                    <xsl:attribute name="book_orientation"><xsl:value-of select="language/position/@book_orientation" /> 
                    </xsl:attribute>
                    <xsl:element name="note"><xsl:value-of select="language/position/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>

您只需要使用所谓的身份转换模式。 总的来说,您需要两个专用模板来处理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>


非常感谢,也感谢您展示我犯的错误!:)非常感谢,也感谢你展示了我犯的错误!:)