首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用XSLT转换嵌套XML

用XSLT转换嵌套XML
EN

Stack Overflow用户
提问于 2020-10-13 13:41:06
回答 2查看 237关注 0票数 1

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

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
<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,但是由于某种原因,它不会复制位置元素的属性或marginalia_text元素的内容(它将成为转换后的XML文件中的note元素)。知道我做错什么了吗?我想过要引用不同的模板,但这也行不通.

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-13 13:57:38

您只需要使用所谓的身份转换模式。总的来说,您需要两个专用模板来处理marginalia_text元素和元素。其他所有内容都将按原样从源XML文档复制。

XSLT

代码语言:javascript
复制
<?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>
票数 0
EN

Stack Overflow用户

发布于 2020-10-13 13:50:37

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

代码语言:javascript
复制
<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>

看到它在这里工作:https://xsltfiddle.liberty-development.net/nb9PtD1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64336341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档