{"word_count": 652, "character_count": 7285}
&#160;During the lexical master class in Berlin, we worked together with Simonetta Battista and Ellert Johannsson on the Dictionary of Old Norse Prose. One of the little exercises we have done was to use XSLT in order to generate lexical entries automatically from an existing annotated textual corpus of Old Norse.The corpusThe starting point is a corpus encoded according to the TEI guidelines done to the level of token. The core unit in the corpus is the sentence (available under /TEI/text/body/div/p):&#60;s&#62;&#60;w lemma="ljúka" type="sfg3en"&#62;Lýkur&#60;/w&#62;&#60;w lemma="hér" type="aa"&#62;hér&#60;/w&#62;&#60;w lemma="saga" type="nveo"&#62;sögu&#60;/w&#62;&#60;w lemma="grettir" type="nkee-s"&#62;Grettis&#60;/w&#62;&#60;w lemma="ásmundarsonar" type="nkee-s"&#62;Ásmundarsonar&#60;/w&#62;&#60;pc&#62;,&#60;/pc&#62;&#60;w lemma="vor" type="fekee"&#62;vors&#60;/w&#62;&#60;w lemma="samlandi" type="nkfe"&#62;samlanda&#60;/w&#62;&#60;pc&#62;.&#60;/pc&#62;&#60;/s&#62;where each token (&#60;w&#62;) is associated to a reference lemma (@lemma attribute).The task at handThe objective is, for each lemma occurring in the annotated corpus, to create a TEI lexical entry that groups together all sentences from the corpus. Going one step further, we have create a specific TEI document for each entry.The XSLT stylesheetAlthough quite simple, the stylesheet contains several XSLT techniques that we describe in the forthcoming sections.Architecture of the stylesheet and first variablesThe stylesheet relies on a a single template associate to the root element of our corpus:&#60;xsl:template match="/"&#62;…&#60;/xsl:template&#62;We first create the following variables (by mean of the XSLT element &#60;xsl:variable/&#62;): theRoot: stores the root element of the source document so that it can be used later in the loop on lemmas, which looses track of the context node;&#60;xsl:variable name="theRoot" select="."/&#62; folderName: the name of the directory where we will store all the dictionary entry documents ; allLemmas: that contains the sequence of all the different lemmas encountered in the corpus by means of a concise XPath instruction:&#60;xsl:variable name="allLemmas" select="distinct-values(descendant::w/@lemma)"/&#62;The XPath expression operates in two steps: descendant::w/@lemma: extracts all lemma attributes from all words which are descendant of the current node (i.e. the root of our document in the current template) ; the powerful XPath function distinct-values() that creates a sequence of all distinct values occurring in the argument sequence.Creating each entryThe process of creating entries is based upon a main loop iterating on the different lemmas, within which we use &#60;xsl:result-document/&#62; to create an ouput document for each lemma.&#60;xsl:for-each select="$allLemmas"&#62;   &#60;xsl:sort/&#62;   &#60;xsl:result-document href="{$folderName}/{.}.xml" method="xml"&#62;   …   &#60;/xsl :result-document&#62;&#60;/xsl:for-each&#62;We have left here a sorting instruction (&#60;xsl:sort/&#62;) as an illustration on how to use it, knowing that it is less useful when entries are created in separate files.Note the use of the {} notation in attributes to resolve the value of specific XPath fragments which, put together, builds up the name of the output file.For each entry, we generate: a &#60;form&#62; element whose &#60;orth&#62; child element contains the lemma a series of &#60;cit&#62; elements for each detected sentence in the corpus (see below)&#60;entry&#62;   &#60;form type="lemma"&#62;      &#60;orth&#62;         &#60;xsl:value-of select="."/&#62;      &#60;/orth&#62;   &#60;/form&#62;   &#60;xsl:for-each select="$theRoot/descendant::w[@lemma = current()]"&#62;      &#60;cit&#62;         &#60;quote&#62;            &#60;xsl:copy-of select="parent::s"/&#62;         &#60;/quote&#62;      &#60;/cit&#62;   &#60;/xsl:for-each&#62;&#60;/entry&#62;The XPath expression "$theRoot/descendant::w[@lemma = current()] reads: find all the &#60;w&#62; elements descendent of the root element (of the corpus) whose @lemma attribute equals the current context node (i.e. the text value of the lemma from the encompassing &#60;xsl:for-each/&#62;). Note that the example sentence is simply obtained by looking for the parent &#60;s&#62; element of the current &#60;w&#62;.The full XSLTHere below, you can find the full XSLT, where you will notice how the header is built up in taking up some parts of the input corpus.The full set of resources can be found in the DARIAH lexical working group GitHub space (note the @xpath-default-namespace attribute which ensures that all generated element are set in the TEI namespace):&#60;?xml version="1.0" encoding="UTF-8"?&#62;&#60;xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"       version="2.0" xmlns="http://www.tei-c.org/ns/1.0"    xpath-default-namespace="http://www.tei-c.org/ns/1.0"&#62;&#60;xsl:output method="xml" indent="yes"/&#62;&#60;xsl:template match="/"&#62;   &#60;xsl:variable name="theRoot" select="."/&#62;   &#60;xsl:variable name="folderName" select="'Entries'"/&#62;   &#60;xsl:variable name="allLemmas"      select="distinct-values(descendant::w/@lemma)"/&#62;   &#60;xsl:for-each select="$allLemmas"&#62;      &#60;xsl:sort/&#62;      &#60;xsl:result-document href="{$folderName}/{.}.xml" method="xml"&#62;         &#60;TEI&#62;            &#60;teiHeader&#62;               &#60;fileDesc&#62;                  &#60;titleStmt&#62;                     &#60;title&#62;Concordance lexical entry for lemma:                         &#60;xsl:value-of select="."/&#62;                     &#60;/title&#62;                  &#60;/titleStmt&#62;                  &#60;publicationStmt&#62;                      &#60;xsl:copy-of select="$theRoot/descendant::publicationStmt/distributor"/&#62;                      &#60;xsl:copy-of select="$theRoot/descendant::publicationStmt/address"/&#62;                      &#60;xsl:copy-of select="$theRoot/descendant::publicationStmt/availability"/&#62;                 &#60;/publicationStmt&#62;                 &#60;sourceDesc&#62;                    &#60;bibl&#62;                       &#60;xsl:copy-of select="$theRoot/descendant::titleStmt/title"/&#62;                   &#60;/bibl&#62;                 &#60;/sourceDesc&#62;              &#60;/fileDesc&#62;            &#60;/teiHeader&#62;            &#60;text&#62;               &#60;body&#62;                  &#60;entry&#62;                     &#60;form type="lemma"&#62;                        &#60;orth&#62;                           &#60;xsl:value-of select="."/&#62;                        &#60;/orth&#62;                     &#60;/form&#62;                     &#60;xsl:for-each select="$theRoot/descendant::w[@lemma = current()]"&#62;                        &#60;cit&#62;                           &#60;quote&#62;                              &#60;xsl:copy-of select="parent::s"/&#62;                           &#60;/quote&#62;                        &#60;/cit&#62;                     &#60;/xsl:for-each&#62;                  &#60;/entry&#62;               &#60;/body&#62;            &#60;/text&#62;         &#60;/TEI&#62;      &#60;/xsl:result-document&#62;   &#60;/xsl:for-each&#62;&#60;/xsl:template&#62;&#60;/xsl:stylesheet&#62;&#160;&#160;