[英]XSLT: get a text node and insert as attribute value of multiple elements
我正在处理DublinCore元数据系统描述的一堆书目条目(更确切地说是期刊文章)。 我想做的就是检索这些数据,并以一致的XML结构重新排列它们,以便创建符合PKP规范并可以上传到我的OJS安装程序的XML文件。
尽管我之前从未接触过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>
等等...
基本上,我想为每个<articleItem/>
复制<language/>
文本节点,并将其粘贴为articleItem
, galley
(HTML)和galley
(PDF)元素的属性值(标有##)。
我尝试了很多不同的解决方案,但没有一个对我有用:实际上,我想知道是否有可能获得此结果...
所以我有点卡在这里; 任何帮助,将不胜感激。 先感谢您。
================================================== ======================
在@ michael.hor257k发表评论后编辑 :
这是我的尝试,但是我知道这根本没有任何意义,因为我不是在操作,而只是创建一个已经存在的元素的副本...
<?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
元素,但是它的文本节点现在是articleItem
[language =“ zh_cn | fr | es”], galley
(HTML)和galley
(PDF)的属性值[both => locale =“ en | fr | es“]
尝试这种方式:
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>
(无“ language”属性)和<galley>...</galley>
(无“ locale”属性)
在这种情况下,模板将必须匹配父元素( articleItem
或galley
),将其复制,复制现有属性,添加/覆盖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.