XSLT:获取文本节点并作为多个元素的属性值插入
XSLT: get a text node and insert as attribute value of multiple elements
我正在处理 DublinCore 元数据系统描述的一堆书目条目(更准确地说,是期刊文章)。我想要做的是检索这些数据并以一致的 XML 结构重新排列它们,以便创建满足 PKP specifications and can be uploaded to my 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
、[=的属性值(用##标记) 16=] (HTML) 和 galley
(PDF)。
我尝试了很多不同的解决方案,但是 none 对我有用:实际上我想知道是否有可能得到这个结果...
所以我有点卡在这里;任何帮助,将不胜感激。提前谢谢你。
============================================= ===========================
在 @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="en|fr|es"], galley
的属性值 ( HTML) 和 galley
(PDF) [均 => 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>
已添加:
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
或 galley
),复制它,复制现有属性,add/overwrite language
属性并将模板应用于子节点 - 例如:
<xsl:template match="articleItem">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="language" select="../language"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
我正在处理 DublinCore 元数据系统描述的一堆书目条目(更准确地说,是期刊文章)。我想要做的是检索这些数据并以一致的 XML 结构重新排列它们,以便创建满足 PKP specifications and can be uploaded to my 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
、[=的属性值(用##标记) 16=] (HTML) 和 galley
(PDF)。
我尝试了很多不同的解决方案,但是 none 对我有用:实际上我想知道是否有可能得到这个结果...
所以我有点卡在这里;任何帮助,将不胜感激。提前谢谢你。
============================================= ===========================
在 @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="en|fr|es"], galley
的属性值 ( HTML) 和 galley
(PDF) [均 => 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>
已添加:
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
或 galley
),复制它,复制现有属性,add/overwrite language
属性并将模板应用于子节点 - 例如:
<xsl:template match="articleItem">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="language" select="../language"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>