简体   繁体   中英

XSLT: get a text node and insert as attribute value of multiple elements

I'm dealing with a bunch of bibliographical entries (to be more precise, journal articles) described by the DublinCore metadata system. What I'm trying to do is retrieve these data and rearrange them in a consistent XML structure, in order to create XML files which meet the PKP specifications and can be uploaded to my OJS installation.

Although I had never approached XSLT before, after a couple of days of studying, this is what I managed to get:

<?xml version="1.0" encoding="UTF-8"?>
<issue>
    <articleItem locale="it_IT" public_id="10.4000/journal.0123" language="##">
        <language>en</language>
        <id type="doi">10.4000/journal.0123</id>
        <title locale="it_IT">My title</title>
        <abstract locale="it_IT">My abstract (Italian).</abstract>
        <abstract locale="en_US">My abstract (English).</abstract>
        <indexing>
            <subject locale="it_IT">my; five; keywords; in; Italian</subject>
            <subject locale="en_US">my; five; keywords; in; English</subject>
        </indexing>
        <author primary_contact="true">
            <lastname>Doe</lastname>
            <firstname>John</firstname>
            <email />
        </author>
        <date_published>2015-10-29</date_published>
        <permissions>
            <copyright_holder locale="it_IT">Journal title</copyright_holder>
            <copyright_year>2015</copyright_year>
        </permissions>
        <galley locale="##">
            <label>HTML</label>
            <file>
                <remote src="http://url-to-my-html" />
            </file>
        </galley>
        <galley locale="##">
            <label>PDF</label>
            <file>
                <href src="http://url-to-my-pdf" mime_type="application/pdf" />
            </file>
        </galley>
    </articleItem>    

    <articleItem locale="it_IT" public_id="10.4000/journal.4567" language="##">
        <language>fr</language>
        <id type="doi">10.4000/journal.4567</id>
        <title locale="it_IT">My title</title>
        <abstract locale="it_IT">My abstract (Italian).</abstract>
        <abstract locale="en_US">My abstract (English).</abstract>
        <abstract locale="fr_FR">My abstract (French).</abstract>
        <indexing>
            <subject locale="it_IT">my; five; keywords; in; Italian</subject>
            <subject locale="en_US">my; five; keywords; in; English</subject>
            <subject locale="fr_FR">my; five; keywords; in; French</subject>
        </indexing>
        <author primary_contact="true">
            <lastname>Le Blanc</lastname>
            <firstname>François</firstname>
            <email />
        </author>
        <date_published>2015-10-29</date_published>
        <permissions>
            <copyright_holder locale="it_IT">Journal title</copyright_holder>
            <copyright_year>2015</copyright_year>
        </permissions>
        <galley locale="##">
            <label>HTML</label>
            <file>
                <remote src="http://url-to-my-html" />
            </file>
        </galley>
        <galley locale="##">
            <label>PDF</label>
            <file>
                <href src="http://url-to-my-pdf" mime_type="application/pdf" />
            </file>
        </galley>
    </articleItem>    

    <articleItem locale="it_IT" public_id="10.4000/journal.8910" language="##">
        <language>es</language>
        <id type="doi">10.4000/journal.8910</id>
        <title locale="it_IT">My title</title>
        <abstract locale="it_IT">My abstract (Italian).</abstract>
        <abstract locale="en_US">My abstract (English).</abstract>
        <abstract locale="es_ES">My abstract (Spanish).</abstract>
        <indexing>
            <subject locale="it_IT">my; five; keywords; in; Italian</subject>
            <subject locale="en_US">my; five; keywords; in; English</subject>
            <subject locale="es_ES">my; five; keywords; in; Spanish</subject>
        </indexing>
        <author primary_contact="true">
            <lastname>Gonzales</lastname>
            <firstname>Juan</firstname>
            <email />
        </author>
        <date_published>2015-10-29</date_published>
        <permissions>
            <copyright_holder locale="it_IT">Journal title</copyright_holder>
            <copyright_year>2015</copyright_year>
        </permissions>
        <galley locale="##">
            <label>HTML</label>
            <file>
                <remote src="http://url-to-my-html" />
            </file>
        </galley>
        <galley locale="##">
            <label>PDF</label>
            <file>
                <href src="http://url-to-my-pdf" mime_type="application/pdf" />
            </file>
        </galley>
    </articleItem>
</issue>

and so on...

Basically I would like to copy the <language/> text node for each <articleItem/> and paste it as the attribute value (marked with ##) of the elements articleItem , galley (HTML) and galley (PDF).

I tried a lot of different solutions, but none of them worked for me: actually I'm wondering whether it is possible to get this result...

So I'm a little bit stuck here; any help would be appreciated. Thank you in advance.

========================================================================

edit after @michael.hor257k posted a comment:

This is my try, but I know that it doesn't make sense at all, because I'm not manipulating, but just creating a copy of an element that already exists...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="UTF-8" version="1.0" doctype-public="-//PKP//OJS Articles and Issues XML//EN" doctype-system="http://pkp.sfu.ca/ojs/dtds/2.4.7/native.dtd"/>    

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

    <xsl:template match="language">
        <articleItem locale="it_IT" public_id="" language="{node()}"/>
    </xsl:template>
</xsl:stylesheet>

I'm not able to solve this problem:

  1. match two “things” at the same time (the text node which I want to copy AND the target region for pasting);
  2. get the first and print it in different locations...

This is the result that I'm trying to get:

<?xml version="1.0" encoding="UTF-8"?>
<issue>
    <articleItem locale="it_IT" public_id="10.4000/journal.0123" language="en">
        <id type="doi">10.4000/journal.0123</id>
        <title locale="it_IT">My title</title>
        <abstract locale="it_IT">My abstract (Italian).</abstract>
        <abstract locale="en_US">My abstract (English).</abstract>
        <indexing>
            <subject locale="it_IT">my; five; keywords; in; Italian</subject>
            <subject locale="en_US">my; five; keywords; in; English</subject>
        </indexing>
        <author primary_contact="true">
            <lastname>Doe</lastname>
            <firstname>John</firstname>
            <email />
        </author>
        <date_published>2015-10-29</date_published>
        <permissions>
            <copyright_holder locale="it_IT">Journal title</copyright_holder>
            <copyright_year>2015</copyright_year>
        </permissions>
        <galley locale="en">
            <label>HTML</label>
            <file>
                <remote src="http://url-to-my-html" />
            </file>
        </galley>
        <galley locale="en">
            <label>PDF</label>
            <file>
                <href src="http://url-to-my-pdf" mime_type="application/pdf" />
            </file>
        </galley>
    </articleItem>    

    <articleItem locale="it_IT" public_id="10.4000/journal.4567" language="fr">
        <id type="doi">10.4000/journal.4567</id>
        <title locale="it_IT">My title</title>
        <abstract locale="it_IT">My abstract (Italian).</abstract>
        <abstract locale="en_US">My abstract (English).</abstract>
        <abstract locale="fr_FR">My abstract (French).</abstract>
        <indexing>
            <subject locale="it_IT">my; five; keywords; in; Italian</subject>
            <subject locale="en_US">my; five; keywords; in; English</subject>
            <subject locale="fr_FR">my; five; keywords; in; French</subject>
        </indexing>
        <author primary_contact="true">
            <lastname>Le Blanc</lastname>
            <firstname>François</firstname>
            <email />
        </author>
        <date_published>2015-10-29</date_published>
        <permissions>
            <copyright_holder locale="it_IT">Journal title</copyright_holder>
            <copyright_year>2015</copyright_year>
        </permissions>
        <galley locale="fr">
            <label>HTML</label>
            <file>
                <remote src="http://url-to-my-html" />
            </file>
        </galley>
        <galley locale="fr">
            <label>PDF</label>
            <file>
                <href src="http://url-to-my-pdf" mime_type="application/pdf" />
            </file>
        </galley>
    </articleItem>    

    <articleItem locale="it_IT" public_id="10.4000/journal.8910" language="es">
        <id type="doi">10.4000/journal.8910</id>
        <title locale="it_IT">My title</title>
        <abstract locale="it_IT">My abstract (Italian).</abstract>
        <abstract locale="en_US">My abstract (English).</abstract>
        <abstract locale="es_ES">My abstract (Spanish).</abstract>
        <indexing>
            <subject locale="it_IT">my; five; keywords; in; Italian</subject>
            <subject locale="en_US">my; five; keywords; in; English</subject>
            <subject locale="es_ES">my; five; keywords; in; Spanish</subject>
        </indexing>
        <author primary_contact="true">
            <lastname>Gonzales</lastname>
            <firstname>Juan</firstname>
            <email />
        </author>
        <date_published>2015-10-29</date_published>
        <permissions>
            <copyright_holder locale="it_IT">Journal title</copyright_holder>
            <copyright_year>2015</copyright_year>
        </permissions>
        <galley locale="es">
            <label>HTML</label>
            <file>
                <remote src="http://url-to-my-html" />
            </file>
        </galley>
        <galley locale="es">
            <label>PDF</label>
            <file>
                <href src="http://url-to-my-pdf" mime_type="application/pdf" />
            </file>
        </galley>
    </articleItem>
</issue>

Note that the language element is not displayed anymore, but its text node is now the attribute value of articleItem [language="en|fr|es"], galley (HTML) and galley (PDF) [both => locale="en|fr|es"]

Try it this way:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<!-- populate articleItem's language -->
<xsl:template match="articleItem/@language">
    <xsl:attribute name="language" select="../language"/>
</xsl:template>

<!-- populate galley's locale -->
<xsl:template match="galley/@locale">
    <xsl:attribute name="locale" select="ancestor::articleItem/language"/>
</xsl:template>

<!-- remove language -->
<xsl:template match="language"/>

</xsl:stylesheet>

Added:

but what if the attribute name and its value were not set? For instance: <articleItem locale="it_IT">...</articleItem> (without "language" attribute) and <galley>...</galley> (without "locale" attribute)

In such case the template would have to match the parent element ( articleItem or galley ), copy it, copy the existing attributes, add/overwrite the language attribute and apply templates to child nodes - for example:

<xsl:template match="articleItem">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:attribute name="language" select="../language"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM