Miguel Las Heras
Miguel Las Heras

Reputation: 19

search element in XML if it is preceded by a element through XSLT

I'm doing a study of a text and I would like to obtain through a stylesheet XSLT a certain function (specifically, <phr function="adversative">) only if it is preceded by a full stop (<pc>). How can I put this condition on the XSLT stylesheet? I have tried with previously sibling::, but it does not return anything. Here are the stylesheet XSLT and the XML that I created:

<?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>

Upvotes: 0

Views: 126

Answers (2)

Tim C
Tim C

Reputation: 70618

The reason your current expression does not work is because there is a whitespace node between the pc node and the phr, which is what is being picked up by your expression.

One way to solve this is to tell XSLT to ignore whitespace nodes under all elements, by using xsl:strip-space

<xsl:strip-space elements="*" />

Alternatively, change your expression to only check elements, rather than nodes

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

Upvotes: 0

Aniket V
Aniket V

Reputation: 3247

Instead of

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

try

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

This gives the following output

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

The following link Concept XML XLST preceding-sibling and ancestor provides a good explanation on the preceding-sibling axis.

Upvotes: 1

Related Questions