需要从TEI-XML文件php或xsl中解析一些数据(以及如何解析?)


Need to parse some data from TEI-XML files - php or xsl (and how?)

我的网站上有很多XML(TEI)文件,当对它们进行可视化时,我想在Infobox中显示文档的一些元信息。我在PHP方面非常有限,所以我第一次考虑用XSL预处理XML,但到目前为止,我未能重新排列输出中项目的顺序。由于xml文件无论如何都在服务器上,我决定搜索有关在PHP中解析xml的教程。但是教程总是使用极其有限的XML文件,而且我未能概括规则。所以我仍然不确定该走哪条路。

我发布了一个文件的标题部分,其中包含所需的信息,下面是我设想的输出。

    <TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="ahne1539">
 <teiHeader>
  <fileDesc>
   <titleStmt>
    <title level="s">Edición digital</title>
    <title type="main" level="a">Cristóbal Márquez a su madre. <lb/>Guatemala. 2 de
     enero de 1813</title>
    <editor>
     <persName>Werner Stangl </persName>
    </editor>
   </titleStmt>
   <publicationStmt>
    <date when="2013">2013</date>
   </publicationStmt>
   <sourceDesc>
    <msDesc>
     <msIdentifier>
      <country>España</country>
      <settlement>Madrid</settlement>
      <repository>Archivo Histórico Nacional</repository>
      <idno>Estado 1539,doc.3</idno>
     </msIdentifier>
     <physDesc>
      <objectDesc>
       <supportDesc>
        <extent> 1 folio, r-v, y un sobre</extent>
       </supportDesc>
       <layoutDesc>
        <p> Orientación horizontal, doblada</p>
       </layoutDesc>
      </objectDesc>
      <handDesc hands="1">
       <p> Letra algo gruesa, regularmente legible</p>
      </handDesc>
     </physDesc>
    </msDesc>
   </sourceDesc>
  </fileDesc>
  <encodingDesc>
  [...]
  </encodingDesc>
  <profileDesc>
   <langUsage>
    <language ident="es" usage="90">español</language>
<language ident="ca" usage="10">catalán</language>
   </langUsage>
  </profileDesc>
 </teiHeader>

所需输出为

<h1>[tei:title with type="main"]</h1> 
<h2>[tei:repository], [tei:idno]</h2> 
<p>Editado por [tei:editor]</p>
<p>Extensión: [tei:extent]</p>
<p>Plano: [tei:layoutDesc]</p>
<p>Letra: [handDesc]</p> 
<p>Lengua: [value of @usage]% [tei:language]</p> 

在最后一行中,实际上应该同时显示加泰罗尼亚语和西班牙语条目

尝试将此作为起点:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="tei">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="tei:TEI/tei:teiHeader/tei:fileDesc">
<h1>
    <xsl:value-of select="tei:titleStmt/tei:title[@type='main']"/>
</h1> 
<h2>
    <xsl:value-of select="tei:sourceDesc/tei:msDesc/tei:msIdentifier/tei:repository"/>
    <xsl:text>, </xsl:text>
    <xsl:value-of select="tei:sourceDesc/tei:msDesc/tei:msIdentifier/tei:idno"/>
</h2> 
<!-- KEEP WORKING ON IT -->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

谢谢。这是我的新代码,它实现了我想要的。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="tei">
    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    <xsl:template match="/">
         <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Metadata</title></head>
                <body>
        <xsl:for-each select="tei:TEI/tei:teiHeader">
            <h1>
                <xsl:value-of select="tei:fileDesc/tei:titleStmt/tei:title[@type='main']"/>
            </h1> 
            <h2>
                <xsl:value-of select="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:msIdentifier/tei:repository"/>
                <xsl:text>, </xsl:text>
                <xsl:value-of select="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:msIdentifier/tei:idno"/>
            </h2> 
            <p>
                Editado por <xsl:value-of select="tei:fileDesc/tei:titleStmt/tei:editor"/>
            </p>
            <hr/>
                    <p>
                        Dimensión: <xsl:value-of select="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc/tei:objectDesc/tei:supportDesc/tei:extent"/>
                    </p>
            Diseño: <xsl:value-of select="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc/tei:objectDesc/tei:layoutDesc/tei:p"/>
            <p>
                <xsl:if test="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc/tei:handDesc/@hands=1">Letra: <xsl:value-of select="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc/tei:handDesc"/></xsl:if><xsl:if test="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc/tei:handDesc/@hands>1"><xsl:value-of select="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc/tei:handDesc/@hands"/> letras: <xsl:value-of select="tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc/tei:handDesc"/>
            </xsl:if>
              </p>     
            <p>Lengua(s): 
                <xsl:for-each select="tei:profileDesc/tei:langUsage/tei:language">
                    <xsl:value-of select="@usage"/>% <xsl:value-of select="."/>
                </xsl:for-each>
            </p>          
        </xsl:for-each>
                </body>
            </html>
    </xsl:template>