简体   繁体   English

XSLT:获取文本节点并作为多个元素的属性值插入

[英]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. 我正在处理DublinCore元数据系统描述的一堆书目条目(更确切地说是期刊文章)。 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. 我想做的就是检索这些数据,并以一致的XML结构重新排列它们,以便创建符合PKP规范并可以上传到我的OJS安装程序的XML文件。

Although I had never approached XSLT before, after a couple of days of studying, this is what I managed to get: 尽管我之前从未接触过XSLT,但经过几天的学习,这是我设法获得的:

<?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). 基本上,我想为每个<articleItem/>复制<language/>文本节点,并将其粘贴为articleItemgalley (HTML)和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: @ michael.hor257k发表评论后编辑

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"] 请注意,不再显示language元素,但是它的文本节点现在是articleItem [language =“ zh_cn | fr | es”], galley (HTML)和galley (PDF)的属性值[both => locale =“ en | fr | es“]

Try it this way: 尝试这种方式:

XSLT 2.0 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) 例如: <articleItem locale="it_IT">...</articleItem> (无“ language”属性)和<galley>...</galley> (无“ locale”属性)

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: 在这种情况下,模板将必须匹配父元素( articleItemgalley ),将其复制,复制现有属性,添加/覆盖language属性并将模板应用于子节点-例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XSLT获取同一标签内多个元素的文本值 - XSLT get the text value of multiple elements within the same tag 无法在XSLT中的祖先节点上获取Attribute的值 - Cannot get value of Attribute on a Ancestor node in XSLT 当它们是文档xslt 2.0中的多个元素时,如何根据属性条件值获取第一个元素值 - How to get first element value depending on attribute condition value when they are multiple elements in document xslt 2.0 XSLT:将 XML 属性转换为元素,然后将该值填充到孙节点 - XSLT: transform XML attribute to elements and then, populate that value to grandchild node 根据 xslt 中的另一个节点文本获取节点的文本值 - Get text value of node based on another node text in xslt 如何使用 XSLT 1.0 的列表的具体元素的文本创建属性值 - how to create attribute value with the text of specifics elements of a list with XSLT 1.0 XSLT使用value-of从具有属性的节点检索文本 - XSLT using value-of to retrieve text from node with attribute XSLT - 仅选择具有特定属性值的元素(但获取全部) - XSLT - select only elements with a specific attribute value (but get all) XSLT如何获取以下兄弟节点的属性值? - XSLT How to get an attribute value of the following-sibling node? 使用xslt从XML的多个属性值中获取一个值 - Get a single value from multiple attribute values of XML using xslt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM