Difference between revisions of "GetFragment.xsl"

From TEIWiki
Jump to navigation Jump to search
(New page: == Authorship == {| | ''Author'' | Josef Willenborg, Max Planck Institute for the History of Science, [mailto:jwillenborg@mpiwg-berlin.mpg.de jwillenborg@mpiwg-berlin.mpg.de] |- | ''Last...)
 
Line 1: Line 1:
 +
[[Category:XSLT]]
 
== Authorship ==
 
== Authorship ==
 
{|  
 
{|  
Line 13: Line 14:
  
 
== Summary ==
 
== Summary ==
Input of the transformation script is the XML document of which a fragment should be extracted. Parameters of the transformation script are:
+
Returns an xml fragment between two milestone elements. The result sequence is wrapped into a parent element node.  
 +
 
 +
== Required input ==
 +
XSLT parameters:
 +
<pre><nowiki>
 
* ms1Name (first milestone name: e.g. "pb")
 
* ms1Name (first milestone name: e.g. "pb")
* ms1Position (first milestone position: e.g. 5)
+
* ms1Position (first milestone position: e.g. 1)
 
* ms2Name (second milestone name: e.g. "pb")
 
* ms2Name (second milestone name: e.g. "pb")
* ms2Position (second milestone position: e.g. 6)
+
* ms2Position (second milestone position: e.g. 2)
It returns an xml fragment between two milestone elements. The result sequence is wrapped into a parent element node.  
+
</nowiki></pre>
 +
XML document:
 +
<pre><nowiki>
 +
<TEI xmlns="http://www.tei-c.org/ns/1.0">
 +
  <text>
 +
    <body>
 +
      <pb n="1"/>
 +
      <p>
 +
        <s>This is the first sentence of the first paragraph.</s>
 +
        <s>This is the second sentence of the first paragraph.</s>
 +
        <s>This is the third sentence of the first paragraph.</s>
 +
      </p>
 +
      <pb n="2"/>
 +
      <p>
 +
        <s>This is the first sentence of the first paragraph.</s>
 +
        <s>This is the second sentence of the first paragraph.</s>
 +
        <s>This is the third sentence of the first paragraph.</s>
 +
      </p>
 +
    </body>
 +
  </text>
 +
</TEI>
 +
 
 +
</nowiki></pre>
 +
 
 +
== Expected output ==
 +
<pre><nowiki>
 +
<TEI>
 +
  <text>
 +
    <body>
 +
      <pb n="1"/>
 +
      <p>
 +
        <s>This is the first sentence of the first paragraph.</s>
 +
        <s>This is the second sentence of the first paragraph.</s>
 +
        <s>This is the third sentence of the first paragraph.</s>
 +
      </p>
 +
      <pb n="2"/>
 +
    </body>
 +
  </text>
 +
</TEI>
 +
 
 +
</nowiki></pre>
  
 
== Restrictions ==
 
== Restrictions ==

Revision as of 15:08, 12 July 2011

Authorship

Author Josef Willenborg, Max Planck Institute for the History of Science, jwillenborg@mpiwg-berlin.mpg.de
Last revised 2011-07-12
Previous version none


Summary

Returns an xml fragment between two milestone elements. The result sequence is wrapped into a parent element node.

Required input

XSLT parameters:

* ms1Name (first milestone name: e.g. "pb")
* ms1Position (first milestone position: e.g. 1)
* ms2Name (second milestone name: e.g. "pb")
* ms2Position (second milestone position: e.g. 2)

XML document:

<TEI xmlns="http://www.tei-c.org/ns/1.0">
  <text>
    <body>
      <pb n="1"/>
      <p>
        <s>This is the first sentence of the first paragraph.</s>
        <s>This is the second sentence of the first paragraph.</s>
        <s>This is the third sentence of the first paragraph.</s>
      </p>
      <pb n="2"/>
      <p>
        <s>This is the first sentence of the first paragraph.</s>
        <s>This is the second sentence of the first paragraph.</s>
        <s>This is the third sentence of the first paragraph.</s>
      </p>
    </body>
  </text>
</TEI>

Expected output

<TEI>
  <text>
    <body>
      <pb n="1"/>
      <p>
        <s>This is the first sentence of the first paragraph.</s>
        <s>This is the second sentence of the first paragraph.</s>
        <s>This is the third sentence of the first paragraph.</s>
      </p>
      <pb n="2"/>
    </body>
  </text>
</TEI>

Restrictions

  • uses the Saxon extension function "saxon:evaluate" (so the Saxon library is needed for this)
  • takes no care about namespaces
  • wrapping into parent node cannot be configured

Code

<?xml version="1.0"?>
<!-- Delivers the fragment between two milestones. Takes no care about namespaces. -->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="local-function">

<xsl:function name="fn:contains">
  <xsl:param name="sequence" as="node()*"/> 
  <xsl:param name="node" as="node()?"/> 
  <xsl:sequence select="some $nodeInSequence in $sequence satisfies $nodeInSequence is $node"/>
</xsl:function>

<xsl:output method="xml" encoding="utf-8"/>

<!-- ms1Name and ms2Name have to be given without namespace: e.g. "pb" -->
<xsl:param name="ms1Name"></xsl:param>
<xsl:param name="ms1Position"></xsl:param>
<xsl:param name="ms2Name"></xsl:param>
<xsl:param name="ms2Position"></xsl:param>

<xsl:variable name="ms1XPath" select="concat('subsequence(//*:', $ms1Name, ', ', $ms1Position, ', 1)')"/>
<xsl:variable name="ms2XPath" select="concat('subsequence(//*:', $ms2Name, ', ', $ms2Position, ', 1)')"/>
<xsl:variable name="ms1" select="saxon:evaluate($ms1XPath)" xmlns:saxon="http://saxon.sf.net/"/>
<xsl:variable name="ms2" select="saxon:evaluate($ms2XPath)" xmlns:saxon="http://saxon.sf.net/"/>
<xsl:variable name="ms1Ancestors" select="$ms1/ancestor::*"/>
<xsl:variable name="ms2Ancestors" select="$ms2/ancestor::*"/>

<xsl:template match="element()[local-name() != $ms1Name and local-name() != $ms2Name]">
  <xsl:choose>
    <xsl:when test="(. >> $ms1 or fn:contains($ms1Ancestors, .)) and ($ms2 >> . or fn:contains($ms2Ancestors, .))">
      <xsl:element name="{local-name(.)}"><xsl:apply-templates/></xsl:element>
    </xsl:when>
    <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="attribute()|text()|comment()|processing-instruction()">
  <xsl:choose>
    <xsl:when test=". >> $ms1 and $ms2 >> .">
      <xsl:copy><xsl:apply-templates/></xsl:copy>
    </xsl:when>
    <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="element()[local-name() = $ms1Name or local-name() = $ms2Name]">
  <xsl:choose>
    <xsl:when test=". is $ms1">
      <xsl:element name="{local-name(.)}"><xsl:copy-of select="@*"></xsl:copy-of></xsl:element>
    </xsl:when>
    <xsl:when test=". is $ms2">
      <xsl:element name="{local-name(.)}"><xsl:copy-of select="@*"></xsl:copy-of></xsl:element>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>