<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="4.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:map="http://www.w3.org/2005/xpath-functions/map"
  xmlns:math="http://www.w3.org/2005/xpath-functions/math"
  xmlns:array="http://www.w3.org/2005/xpath-functions/array"
  xmlns:tm="http://www.datenverdrahten.de/tm"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <!-- by T. Meinike 11/22 -->

  <xsl:output method="xml" version="1.0" html-version="5" encoding="UTF-8"
    indent="yes" include-content-type="no" omit-xml-declaration="yes"/>

  <xsl:item-type name="point_record" as="record(x as xs:double, y as xs:double)"/>

  <xsl:function name="tm:point" as="type(point_record)">
    <xsl:param name="x" as="xs:double"/>
    <xsl:param name="y" as="xs:double"/>
    <xsl:sequence select="map{'x':$x, 'y':$y}"/>
  </xsl:function>

  <!-- Abstand zwischen zwei Punkten -->
  <xsl:function name="tm:distance" as="xs:double">
    <xsl:param name="point1" as="type(point_record)"/>
    <xsl:param name="point2" as="type(point_record)"/>
    <xsl:sequence select="math:sqrt(math:pow($point1?x - $point2?x, 2) + math:pow($point1?y - $point2?y, 2))"/>
  </xsl:function>

  <xsl:template name="xsl:initial-template">

    <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html>&#xA;</xsl:text>
    <html lang="de">
      <head>
        <meta charset="UTF-8"/>
        <title>xsl:item-type mit record()</title>
        <style>* {{ font-family: sans-serif; }}</style>
      </head>
      <body>
        <h1>xsl:item-type mit record()</h1>

        <xsl:variable name="p1" select="tm:point(75, 50)" as="type(point_record)"/>
        <xsl:variable name="p2" select="tm:point(300, 150)" as="type(point_record)"/>
        <xsl:variable name="abstand" select="tm:distance($p1, $p2)" as="xs:double"/>

        <svg xmlns="http://www.w3.org/2000/svg" width="500" height="250">
          <text x="0" y="30" font-size="20">Abstand zwischen zwei Punkten: {fn:round($abstand, 2)}</text>
          <line x1="{$p1?x}" y1="{$p1?y}" x2="{$p2?x}" y2="{$p2?y}" stroke="#F00" stroke-width="2"/>
          <circle cx="{$p1?x}" cy="{$p1?y}" r="2" fill="#00F"/>
          <circle cx="{$p2?x}" cy="{$p2?y}" r="2" fill="#00F"/>
        </svg>
      </body>
    </html>

  </xsl:template>

</xsl:stylesheet>