[英]Transforming nested XML with XSLT
I seemed to have made some progress with XSLT, but I'm still finding it very hard to transform nested XML.我似乎在使用 XSLT 方面取得了一些进展,但我仍然发现转换嵌套 XML 非常困难。 The XML looks as follows:
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>
By retaining some elements and attributes and modifying others I want to slightly transform this to:通过保留一些元素和属性并修改其他元素和属性,我想将其稍微转换为:
<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>
I have generated this XSL so far, but for some reason it won't copy the properties of the position element nor the content of the marginalia_text element (which becomes the note element in the transformed XML file).到目前为止,我已经生成了这个 XSL,但由于某种原因,它不会复制 position 元素的属性,也不会复制 marginia_text 元素(它成为转换后的 XML 文件中的 note 元素)的内容。 Any idea what I'm doing wrong?
知道我做错了什么吗? I thought about referring to various templates, but this doesn't work either...
我想过参考各种模板,但这也不起作用......
<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>
You were only missing the path to the attributes or elements you needed.您只是缺少指向所需属性或元素的路径。 Remember that your template matches a "marginalia" node.
请记住,您的模板与“边缘”节点相匹配。 From there you have to specify the path to your elements/attributes.
从那里您必须指定元素/属性的路径。
<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>
See it working here : https://xsltfiddle.liberty-development.net/nb9PtD1看到它在这里工作: https : //xsltfiddle.liberty-development.net/nb9PtD1
You just need to use so called Identity Transformation pattern.您只需要使用所谓的身份转换模式。 Overall, you need two dedicated templates to handle marginalia and marginalia_text elements.
总体而言,您需要两个专用模板来处理marginia和marginia_text元素。 Everything else will be copied as-is from the source XML document.
其他所有内容都将按原样从源 XML 文档复制。
XSLT 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.