XML中的搜索元素,如果它前面有一个通过XSLT的元素

时间:2018-03-19 07:36:58

标签: xml xslt

我正在研究一个文本,我想通过样式表XSLT得到一个特定的函数(具体来说,<phr function="adversative">),只有它前面有一个句号(<pc>) 。如何将此条件放在XSLT样式表上?我试过了previously sibling::,但它没有返回任何内容。以下是样式表XSLT和我创建的XML:

<?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>COORDINADAS ADVERSATIVA</b>
                </h3>
                <table width="750" border="1" align="center">
                    <tr height="150%">
                        <th scope="col" bgcolor="#00CCFF" height="150%">
                            <div align="center">Coordinadas adversativas</div>
                        </th>
                        <th scope="col" bgcolor="#00CCFF" height="150%">
                            <div align="center">Libro</div>
                        </th>
                        <th scope="col" bgcolor="#00CCFF" height="150%">
                            <div align="center">Capítulo</div>
                        </th>
                        <th scope="col" bgcolor="#00CCFF" height="150%">
                            <div align="center">Folio</div>
                        </th>
                        <th scope="col" bgcolor="#00CCFF" height="150%">
                            <div align="center">Columna</div>
                        </th>
                        <th scope="col" bgcolor="#00CCFF" height="150%">
                            <div align="center">Línea comienzo</div>
                        </th>
                        <th scope="col" bgcolor="#00CCFF" height="150%">
                            <div align="center">Línea final</div>
                        </th>
                    </tr>

                    <tr>

                        <td>
                            <xsl:for-each
                                select="//tei:TEI//tei:phr[@function='adversative' and preceding-sibling::node()[1][self::tei:pc]]">
                                <div align="center">
                                    <xsl:value-of select="."/>
                                </div>
                                <hr/>
                            </xsl:for-each>
                        </td>
                        <td>
                            <xsl:for-each select="//tei:TEI//tei:phr[@function = 'adversative']">
                                <div align="center">
                                    <xsl:value-of select="ancestor::tei:div1/@n"/>
                                </div>
                                <hr/>
                            </xsl:for-each>
                        </td>
                        <td>
                            <xsl:for-each select="//tei:TEI//tei:phr[@function = 'adversative']">
                                <div align="center">
                                    <xsl:value-of select="ancestor::tei:div2/@n"/>
                                </div>
                                <hr/>
                            </xsl:for-each>
                        </td>
                        <td>
                            <xsl:for-each select="//tei:TEI//tei:phr[@function = 'adversative']">
                                <div align="center">
                                    <xsl:value-of select="preceding::tei:pb[@n][1]/@n"/>
                                </div>
                                <hr/>
                            </xsl:for-each>
                        </td>
                        <td>
                            <xsl:for-each select="//tei:TEI//tei:phr[@function = 'adversative']">
                                <div align="center">
                                    <xsl:value-of select="preceding::tei:cb[@n][1]/@n"/>
                                </div>
                                <hr/>
                            </xsl:for-each>
                        </td>
                        <td>
                            <xsl:for-each select="//tei:TEI//tei:phr[@function = 'adversative']">
                                <div align="center">
                                    <xsl:value-of select="preceding::tei:lb[@n][1]/@n"/>
                                </div>
                                <hr/>
                            </xsl:for-each>
                        </td>
                        <td>
                            <xsl:for-each select="//tei:TEI//tei:phr[@function = 'adversative']">
                                <div align="center">
                                    <xsl:value-of select="following::tei:lb[@n][1]/@n"/>
                                </div>
                                <hr/>
                            </xsl:for-each>
                        </td>
                    </tr>
                </table>
            </body>
        </html>

    </xsl:template>



</xsl:stylesheet>

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="../18-03-2018%20TEI/XSLT/2_Puntuacion_de_la_oracion/5-coordinada-adversativa.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="006">
          <pb n="003r"/>
          <cb n="b"/>
          <head>De <name type="Antropónimo">abel</name>
            <lb n="28"/>et de su hermano que nascio con el<pc>.</pc></head>
          <ab><phr function="inciso" type="absolute"><lb n="29"/>ANdados <phr type="syntagm"
            function="copulative">treynta annos &amp; se<lb n="30"/>ys dias</phr> de quando el
            mundo fue <lb n="31"/>criado</phr><pc>.</pc>
            <phr function="inciso" type="source" n="luchas y otros">assi como dize <phr
              type="syntagm" function="copulative">mahes<lb n="32"/>tre <name type="Antropónimo"
                >luchas</name> obispo de <name type="place">thuy</name> &amp; otros <phr
                  function="adjetive" type="restrictive">que acuerdan <lb n="33"/>con
                  el</phr></phr></phr><pc>.</pc> fizieron <phr type="syntagm" function="copulative"
                    ><name type="Antropónimo">adam</name> &amp; <name type="Antropónimo"
                      >eua</name></phr> otro fijo<pc>.</pc> ¶ Et a es<lb n="34"/>te segundo fijo dixieron
            <name type="Antropónimo">abel</name><pc>.</pc> Et <name type="Antropónimo">abel</name>
            <phr function="inciso" type="source" n="esponimientos">segund <lb n="35"/>los
              esponimientos dela <name type="book">biblia</name></phr> quiere dezir en <lb n="36"
              />el nuestro lenguage castellano<pc>.</pc>
            <phr function="comparative" type="name" n="abel">tanto como <lb n="37"/>lloro o cosa
              <phr function="adjetive" type="restrictive">que non es
                duradera</phr></phr><pc>.</pc> ¶ <phr function="adjetive" type="explanatory">Onde <lb
                  n="38"/>dize otrossi <name type="Antropónimo">gregorio</name> enla glosa del <name
                    type="book">genesis</name>
                  <lb n="39"/>sobre este logar<pc>.</pc> que <phr function="comparative" type="name"
                    n="abel"><name type="Antropónimo">abel</name> tanto quiere dezir como <lb n="40"
                    />baho<pc>.</pc>
                    <phr function="causal">por que <phr type="sub" function="comparative">assi como el
                      baho <phr type="coord" function="copulative">se ua ayna <lb n="41"/>&amp; se
                        esparze por ell ayre &amp; non paresçe</phr></phr></phr></phr></phr><pc>.</pc>
            <pb n="003v"/>
            <cb n="a"/>
            <lb n="1"/>assi fallescio ayna <name type="Antropónimo">abel</name><pc>.</pc>
            <phr function="inciso" type="cataphoric">&amp; fues commo oyre <lb n="2"/>des &amp; non
              parescio mas</phr><pc>.</pc> ¶ Et con este <name type="Antropónimo">abel</name> na<lb
                n="3"/>çio otra hermana de un parto commo <name type="Antropónimo">cayn</name> et <lb
                  n="4"/>la suya<pc>.</pc> Et esta ouo nombre <name type="Antropónimo">delbora</name>
            <phr function="adjetive" type="restrictive">que mues <lb n="5"/>tra <phr
              function="comparative" type="name" n="delbora">tanto como seguydora</phr>
              <phr function="causal">por que siguio asu <lb n="6"/>hermano <name type="Antropónimo"
                >abel</name></phr></phr><pc>.</pc>
            <phr function="causal">Ca <phr function="inciso" type="source" n="unos">asi commo dizen
              unos</phr>
              <phr type="sub" function="comparative">uis <lb n="7"/>co poco como
                el</phr></phr><pc>.</pc>
            <phr function="concessive">Pero otros cuentan <phr type="coord" function="copulative"
              >que uisco <lb n="8"/>mucho<pc>.</pc> &amp; que <phr function="comparative"
                type="name" n="delbora">tanto commo seguidora quiere dezir <lb n="9"/><name
                  type="Antropónimo">delbora</name></phr></phr></phr><pc>.</pc>
            <phr function="adversative">Non <phr function="causal">por que de muerte ella siguiesse
              <lb n="10"/>assu hermano</phr><pc>.</pc> mas por quel siguio <phr type="syntagm"
                function="copulative">en iusticia <lb n="11"/>&amp; en bondat</phr> amando a <name
                  type="Antropónimo">dios</name></phr><pc>.</pc> Ca departen que <lb n="12"/>tanto
            uisco que alcanço asu hermano <name type="Antropónimo">seth</name><pc>.</pc> que <lb
              n="13"/>nasçio muchos annos despues que <name type="Antropónimo"
                >delbora</name><pc>.</pc> &amp; <lb n="14"/>que ella caso con el<pc>.</pc>
            <phr function="inciso" type="cataphoric">Et desto diremos adelante</phr><pc>.</pc>
          </ab>
        </div2>
      </div1>
    </body>
  </text>
</TEI>

2 个答案:

答案 0 :(得分:1)

而不是

tei:phr[@function='adversative' and preceding-sibling::node()[1][self::tei:pc]]

尝试

tei:phr[@function='adversative' and preceding-sibling::tei:pc]

这给出了以下输出

Non por que de muerte ella siguiesse assu hermano . mas por quel siguio en iusticia & en bondat amando a dios

以下链接Concept XML XLST preceding-sibling and ancestorpreceding-sibling轴提供了很好的解释。

答案 1 :(得分:0)

当前表达式不起作用的原因是因为pc节点和phr之间存在空白节点,这是您的表达式所拾取的节点。

解决此问题的一种方法是通过使用xsl:strip-space

告诉XSLT忽略所有元素下的空白节点
<xsl:strip-space elements="*" />

或者,将表达式更改为仅检查元素,而不是节点

<xsl:for-each 
     select="//tei:TEI//tei:phr[@function='adversative' and preceding-sibling::*[1][self::tei:pc]]">
相关问题