view software/eXist/webapp/mpdl/presentation/insertExternalElements.xsl @ 9:1ec29fdd0db8

neue .lex Dateien f?r Normalisierung / externe Objekte update
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 22 Feb 2011 16:03:45 +0100
parents
children 469d927b9ca7
line wrap: on
line source

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:saxon="http://saxon.sf.net/"
  xmlns:text="http://www.mpiwg-berlin.mpg.de/ns/mpdl/text"
  xmlns:mpdlxmlutil="java:de.mpg.mpiwg.berlin.mpdl.util.XmlUtil"
>

<xsl:output method="xml" encoding="utf-8"/>
<xsl:output name="myXml" indent="yes" omit-xml-declaration="yes"/>

<xsl:variable name="externalElements" select="/result/externalElements"/>
<xsl:variable name="fragment" select="/result/fragment"/>

<xsl:template match="result">
  <xsl:apply-templates/>
</xsl:template>
<xsl:template match="fragment">
  <xsl:apply-templates/>
</xsl:template>
<xsl:template match="externalElements">
</xsl:template>

<xsl:template match="attribute()|text()|comment()|processing-instruction()">
  <xsl:copy>
    <xsl:apply-templates select="attribute()|element()|text()|comment()|processing-instruction()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="element()">
  <xsl:variable name="elemXmlNodeIdTmp" select="saxon:path(.)"/>
  <xsl:variable name="elemXmlNodeId" select="concat('/', substring-after(substring-after(substring-after($elemXmlNodeIdTmp, '/'), '/'), '/'))"/>
  <xsl:variable name="extElemXmlNodeIds" select="$externalElements//@xmlNodeId"/>
  <xsl:variable name="extElemIndex" select="index-of($extElemXmlNodeIds, $elemXmlNodeId)"/>
  <xsl:variable name="extElem" select="$externalElements/*[$extElemIndex]"/>
  <xsl:variable name="extElemContent" select="$extElem/content/*[1]"/>
  <xsl:variable name="extElemContentSerialized" select="saxon:serialize($extElemContent, 'myXml')"/>
  <xsl:variable name="before">
    <xsl:choose>
      <xsl:when test="empty($extElem/@before) or $extElem/@before = ''">
        <xsl:value-of select="''"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="string($extElem/@before)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="charPos">
    <xsl:choose>
      <xsl:when test="empty($extElem/@charPos) or $extElem/@charPos = ''">
        <xsl:value-of select="xs:integer(-1)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="xs:integer($extElem/@charPos)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="elemSerialized" select="saxon:serialize(., 'myXml')"/>
  <xsl:variable name="insertedElemSerialized" select="mpdlxmlutil:insertAtCharPos(mpdlxmlutil:new(), $elemSerialized, string($charPos), $extElemContentSerialized)"/>
  <xsl:variable name="insertedElemDeSerializedTmp" select="saxon:parse($insertedElemSerialized)"/>
  <xsl:variable name="insertedElemDeSerialized">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="attribute()"/>
      <xsl:attribute name="xmlNodeId">
        <xsl:value-of select="$elemXmlNodeId"/>
      </xsl:attribute>
      <xsl:sequence select="$insertedElemDeSerializedTmp/*[1]/node()"/>
    </xsl:element>
  </xsl:variable>
  <xsl:if test="$charPos &lt; 0 and $before = 'true'">
    <xsl:sequence select="$extElemContent"/>
  </xsl:if>
  <xsl:if test="$charPos &gt;= 0">
    <xsl:sequence select="$insertedElemDeSerialized"/>
  </xsl:if>
  <xsl:if test="$charPos &lt; 0">
    <xsl:copy>
      <xsl:apply-templates select="attribute()"/>
      <xsl:attribute name="xmlNodeId">
        <xsl:value-of select="$elemXmlNodeId"/>
      </xsl:attribute>
      <xsl:apply-templates select="element()|text()|comment()|processing-instruction()"/>
    </xsl:copy>
  </xsl:if>
  <xsl:if test="$charPos &lt; 0 and $before = 'false'">
    <xsl:sequence select="$extElemContent"/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>