这必须是一个非常基本的问题,但我真的很难找到解决方案。从以下XML中,我试图仅提取第一个实例,当我在tag="099"
节点中的code="a"
和marc:datafield
属性匹配时,以及{{1 } node,分别为
marc:subfield
所以,我正试图找回第一个“940002”。使用以下代码:
<?xml version="1.0" encoding="UTF-8" ?>
<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<marc:record>
...
<marc:datafield tag="079" ind1=" " ind2=" ">
<marc:subfield code="a">79927839</marc:subfield>
</marc:datafield>
<marc:datafield tag="099" ind1=" " ind2=" ">
<marc:subfield code="a">940002</marc:subfield>
</marc:datafield>
<marc:datafield tag="099" ind1=" " ind2=" ">
<marc:subfield code="a">940002*</marc:subfield>
</marc:datafield>
<marc:datafield tag="099" ind1=" " ind2=" ">
<marc:subfield code="a">940002**</marc:subfield>
...
然而,我得到的是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:marc="http://www.loc.gov/MARC21/slim">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/">
<!--identifier-->
<xsl:if test="/marc:collection/marc:record/marc:datafield/@tag='099'">
<xsl:element name="dc:identifier">
<xsl:if test="position()=1">
<xsl:value-of select="/marc:collection/marc:record/marc:datafield/marc:subfield/@code='a'"/>
</xsl:if>
</xsl:element>
</xsl:if>
...
取代期望的:
<?xml version="1.0" encoding="UTF-8"?>
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:marc="http://www.loc.gov/MARC21/slim">
<dc:identifier>true</dc:identifier>
...
我猜测它必须是一个Xpath问题,但是...任何线索都会受到赞赏。
由于
答案 0 :(得分:2)
更改此
<xsl:value-of select="/marc:collection/marc:record/marc:datafield/marc:subfield/@code='a'"/>
到
<xsl:value-of select="/marc:collection/marc:record/marc:datafield/marc:subfield[@code='a']/text()"/>
您的价值在于选择@code
是否等于'a'
,而不是选择marc:subfield
@code='a'
答案 1 :(得分:2)
使用强>:
/marc:collection/marc:record/marc:datafield
[@tag='099']/marc:subfield[@code='a'][1]/text()
答案 2 :(得分:1)
知道了!添加另一个选择就可以了。非常感谢你的帮助!
<xsl:for-each select="/marc:collection/marc:record/marc:datafield[@tag='099']">
<xsl:element name="dc:identifier">`
<xsl:if test="position()=1">
<xsl:value-of select="marc:subfield[@code='a']/text()"/>
</xsl:if>
</xsl:element>
</xsl:for-each>