【问题标题】:Find value attribute xml xslt查找值属性 xml xslt
【发布时间】:2018-03-13 08:05:31
【问题描述】:

我是新手,刚刚接触到 XSLT。我正在研究一份手稿,我想获得其中的专有名称(@name type ="proper")。通过样式表我只知道如何获取名称(@name)的值我如何添加类型? 以下是我创建的样式表 XSLT 和 XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng" type="application/xml"
	schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-stylesheet type="text/xsl" href="antroponimos2.xsl"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>Title</title>
      </titleStmt>
      <publicationStmt>
        <p>Publication Information</p>
      </publicationStmt>
      <sourceDesc>
        <p>Information about the source</p>
      </sourceDesc>
    </fileDesc>
  </teiHeader>
  <text>
    <body>
      <div1 type="book" n="01">
        <div2 type="chapter" n="000">
          <pb n="001r"/>
          <cb n="a"/>
          <head>
            <hi rend="red"><lb n="1"/>Aqui se comiença <lb n="2"/>la general &amp; grand es<lb n="3"
              />toria que el muy noble <w type="majuscule">Rey</w>
              <lb n="4"/>don alfonso fijo del noble <w type="majuscule">Rey</w>
              <lb n="5"/>don fernando &amp; dela <w type="majuscule">Reyna</w>
              <lb n="6"/>donna beatriz mando fazer. <lb n="7"/>Prólogo.</hi>
          </head>
          <ab>
            <lb n="8"/>
            Et delos fechos delos malos que
              reçibies<lb n="14"/>sen castigo. por se saber guardar delo non fazer.<lb n="15"/>ONde
            por todas estas cosas. yo don<lb n="16"/><name type="proper" n="in">Alfonsso</name> por
            la gracia de dios <phr type="apposition">Rey <phr function="list"><phr function="list"
                  n="1">de<lb n="17"/><name type="place" n="in">Castiella</name></phr>. <phr
                  function="list" n="2">de <name type="place" n="in">Toledo</name></phr>. <phr
                  function="list" n="3">de <name type="place" n="in">Leon</name></phr>. <phr
                  function="list" n="4">de <name type="place" n="in">Gal<lb n="18"
                  />lizia</name></phr>. <phr function="list" n="5">de <name type="place" n="in"
                    >Seuilla</name></phr>. <phr function="list" n="6">de <name type="place" n="in"
                    >Cordoua</name></phr>
            </phr></phr>
          </ab>
        </div2>
      </div1>
    </body>
  </text>
</TEI>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:tei="http://www.tei-c.org/ns/1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html lang="es">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            </head>
            <body>
                <h3 align="center">
                    <b>NOMBRES PROPIOS</b>
                </h3>
                <table width="750" border="1" align="center">
                    <tr>
                        <th scope="col" bgcolor="#00CCFF">
                            <div align="center">Nombre</div>
                        </th>
                    </tr>

                    <tr>

                        <td>
                            <xsl:for-each select="//tei:div1//tei:name">
                                <div align="center">
                                    <xsl:value-of select="."/>
                                </div>
                            </xsl:for-each>
                        </td>
                    </tr>
                </table>
            </body>
        </html>

    </xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 当你使用@name 意味着这是名称属性。

标签: xml xslt


【解决方案1】:

你应该使用name元素和匹配@type属性然后值proper,见下面的代码:

name[@type='proper']

【讨论】:

    【解决方案2】:

    您可以使用以下Xpath之一

    /TEI/text/body/div1/div2/ab/name[@type='proper'] 
    

    强制 DOM 树中的确切级别或

    //name[@type='proper'] 
    

    访问 DOM 树中名称为 name 并具有名为 type 的属性为 proper 的任何元素。

    此外,如果您只需要访问元素的内容(文本),您可以将 /text() 附加到您的 xpath:

    //name[@type='proper']/text() 
    

    /TEI/text/body/div1/div2/ab/name[@type='proper']/text()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 2021-12-20
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2016-12-28
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多