I seemed to have made some progress with XSLT, but I'm still finding it very hard to transform nested XML. The XML looks as follows:
<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). 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
You just need to use so called Identity Transformation pattern. Overall, you need two dedicated templates to handle marginalia and marginalia_text elements. Everything else will be copied as-is from the source XML document.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.