将数据从2个xml文件中提取到第三个文件中

时间:2014-10-27 18:43:15

标签: xpath xslt-2.0

我正在尝试创建一个电子表格/分隔文本文件,该文件使用XSLT 2.0(在Oxygen中使用Saxon-PE 9.4.0.3进行转换)从基于文件名的2个XML文件中提取数据。

文件1(主数据文件),WMS_Arabic_0006_mrc_test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:tei="http://www.tei-c.org/ns/1.0"
   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:leader>00000ntm a22000002a 4500</marc:leader>
      <marc:datafield ind2=" " ind1=" " tag="699">
         <marc:subfield code="a">WMS Arabic 6</marc:subfield>
      </marc:datafield>
      <marc:datafield ind2=" " ind1=" " tag="040">
         <marc:subfield code="b">ara</marc:subfield>
      </marc:datafield>
   </marc:record>
</marc:collection>

文件2(我想在此文件中添加选定的字段),WMS_Arabic_0006_tei_test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<tei:TEI xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:marc="http://www.loc.gov/MARC21/slim"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <tei:teiHeader>
      <tei:fileDesc>
         <tei:titleStmt>
            <tei:title>Library</tei:title>
         </tei:titleStmt>
         <tei:publicationStmt>
            <tei:p>Library</tei:p>
         </tei:publicationStmt>
         <tei:sourceDesc>
            <tei:p>Born Digital</tei:p>
         </tei:sourceDesc>
      </tei:fileDesc>
   </tei:teiHeader>
   <tei:text>
      <tei:body>
         <tei:div>
            <tei:msDesc xml:lang="eng-Latn" xml:id="mWMSArabic6">
               <tei:msIdentifier>
                  <tei:collection type="main">Asian Collection</tei:collection>
                  <tei:collection type="sub">Iskandar</tei:collection>
                  <tei:idno>WMS Arabic 6</tei:idno>
               </tei:msIdentifier>
            </tei:msDesc>
         </tei:div>
      </tei:body>
   </tei:text>
</tei:TEI>

这是XSLT文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
   xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:tei="http://www.tei-c.org/ns/1.0"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
   exclude-result-prefixes="xs" xmlns:wdl="http://project.wdl.org">
   <xsl:import href="../wdlfunclib.xsl"/>
   <xsl:strip-space elements="*"/>
   <!--Output as text-->
   <xsl:output encoding="UTF-8" method="text"/>
   <!-- Add field names on first line-->
   <xsl:template match="/">ID|Collection|
      <xsl:apply-templates/>
   </xsl:template>
   <xsl:template match="marc:record">
      <xsl:variable name="mrcFile_Name"><xsl:value-of select="tokenize(base-uri(.),'/')[last()]"
         /></xsl:variable>
      <xsl:variable name="teiFile_Name">WMS_Arabic_<xsl:value-of
            select="format-number(number(substring-after(substring-after(marc:datafield[@tag='699']/marc:subfield[@code='a'],' '),' ')),'0000')"
         />_tei.xml</xsl:variable>
      <xsl:variable name="teiPath"><xsl:value-of
            select="substring-before(base-uri(.),$mrcFile_Name)"/></xsl:variable>
      <xsl:variable name="teiFile_Location"><xsl:value-of select="$teiPath"/><xsl:value-of
            select="$teiFile_Name"/></xsl:variable>
      <xsl:variable name="myDoc"><xsl:value-of select="document($teiFile_Location)"/></xsl:variable>
      <!-- ID --><xsl:value-of
         select="normalize-space(marc:datafield[@tag='699']/marc:subfield[@code='a'])"
         />|<!-- Collection --><xsl:value-of
         select="$myDoc//tei:TEI/tei:text/tei:body/tei:div/tei:msDesc/tei:msIdentifier/tei:collection"
      />|<xsl:text>&#xA;</xsl:text>
   </xsl:template>
</xsl:stylesheet>

我似乎遇到了Collection语句的问题,特别是路径。如果我使用<xsl:value-of select="$myDoc//.,我会获得所有字段值:

ID|Collection|
    WMS Arabic 6|LibraryLibraryBorn DigitalAsian CollectionIskandarWMS Arabic 6 LibraryLibraryBorn DigitalAsian CollectionIskandarWMS Arabic 6|

所以我知道它与正确的文件相匹配。当我尝试整个集合的路径时,我什么都没得到。我已经尝试了我能想到的每一个变体,但没有任何结果。如果有人有任何想法,那将非常感激。

谢谢, 沙

1 个答案:

答案 0 :(得分:1)

请使用<xsl:variable name="mydoc" select="document($location)"/>代替xsl:value-of内的xsl:variable