<?xml version="1.0" encoding="UTF-8"?>
<x:stylesheet xmlns:x="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eg="http://examplotron.org/0/" xmlns:saxon="http://icl.com/saxon" xmlns:ns1="http://www.w3.org/XML/1998/namespace" xmlns:ns2="http://examplotron.org/0/" version="1.0" ns1:dummy="" ns2:dummy="">
   <x:template match="*">
      <error type="Unexpected element">
         <x:attribute name="path">
            <x:call-template name="getPath"/>
         </x:attribute>
      </error>
   </x:template>
   <x:template match="@*">
      <error type="Unexpected attribute">
         <x:attribute name="path">
            <x:call-template name="getAttPath"/>
         </x:attribute>
      </error>
   </x:template>
   <x:template name="getPath">
      <x:for-each select="parent::*">
         <x:call-template name="getPath"/>
      </x:for-each>
      <x:variable name="name" select="name()"/>
      <x:value-of select="concat('/', $name, '[', count(preceding-sibling::*[name()=$name])+1, ']')"/>
   </x:template>
   <x:template name="getAttPath">
      <x:for-each select="parent::*">
         <x:call-template name="getPath"/>
      </x:for-each>
      <x:variable name="name" select="name()"/>
      <x:value-of select="concat('/@', $name)"/>
   </x:template>
   <x:template priority="1" match="/">
      <x:apply-templates select="*|@*"/>
   </x:template>
   <x:template match="/foo">
      <error type="Element content mismatch" name="/foo" expected="[count(percent)&gt;0 and 1=1 and (sum(percent)=100) and  1=1]"/>
   </x:template>
   <x:template priority="1" match="/foo[count(percent)&gt;0 and 1=1 and (sum(percent)=100) and  1=1]">
      <x:apply-templates select="*|@*"/>
   </x:template>
   <x:template match="/foo/percent">
      <error type="Element content mismatch" name="/foo/percent" expected="[ 1=1]"/>
   </x:template>
   <x:template priority="1" match="/foo/percent[ 1=1]">
      <x:apply-templates select="*|@*"/>
   </x:template>
</x:stylesheet>