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

时间:2015-11-08 02:19:59

标签: xml xslt xslt-1.0 xslt-2.0

我正在处理DublinCore元数据系统描述的一系列书目条目(更确切地说是期刊文章)。我要做的是检索这些数据并以一致的XML结构重新排列它们,以便创建符合PKP specifications的XML文件,并且可以上传到我的OJS安装。

虽然我以前从未接触过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>

依旧......

基本上我想为每个<language/>复制<articleItem/>文本节点,并将其粘贴为元素articleItem的属性值(标有##),{{1} (HTML)和galley(PDF)。

我尝试了很多不同的解决方案,但没有一个能为我工作:实际上我想知道是否有可能得到这个结果......

所以我有点卡在这里;任何帮助,将不胜感激。提前谢谢。

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

@michael.hor257k发表评论后

修改

这是我的尝试,但我知道它根本没有意义,因为我没有操纵,只是创建一个已经存在的元素的副本......

galley

我无法解决这个问题:

  1. 同时匹配两个“东西”(我要复制的文本节点和粘贴的目标区域);
  2. 获取第一个并在不同位置打印...
  3. 这是我想要得到的结果:

    <?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>
    

    请注意,<?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> 元素不再显示,但其文本节点现在是language [language =“en | fr | es”],articleItem的属性值( HTML)和galley(PDF)[both =&gt;区域= “EN | | ES”]

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

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>

加了:

  

但如果未设置属性名称及其值,该怎么办?对于   实例:<articleItem locale="it_IT">...</articleItem>(没有   &#34;语言&#34;属性)和<galley>...</galley>(没有&#34;区域设置&#34;   属性)

在这种情况下,模板必须匹配父元素(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>