Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:52:10+00:00 2026-06-01T11:52:10+00:00

I’m using Schematron to validate instance XML documents inside a custom schema aware XML

  • 0

I’m using Schematron to validate instance XML documents inside a custom schema aware XML editor (note that Schematron validation is just an XSLT transformation). The instance may contain elements with a path in it’s value (a simplified XPath expression). An example of such a path would be:

/p:root/p:level-one/r:level-two/r:level-two-leaf

where both prefixes (p and r) are bound to a namespace defined in the instance document. My Schematron validates such paths by ensuring that an element to which the path points actually exists in the instance document. It relies on EXSLT to do this (I am forced to use XSLT1.0), more precisely on dyn:evaluate() in order to evaluate the element’s text value, which as you can see is basically an XPath expression. It works like a charm.

But there is one problem, a huge one actually. The call to dyn:evaluate() is executed from a Schematron XSLT which evaluates the XPath expression in it’s own namespace context. This means that in order for this to work properly both the instance document and the Schematron XSLT must use exactly the same prefix → namespace binding. I cannot force a user to use the same prefixes for the same namespaces which are specified in my schema…that would be a silly requirement (it is however ensured that at least the same namespaces will be used in both). Schematron is always generated before instance validation takes place but this is done only once for performance reasons. The only option I have is to somehow pre-process the paths from the instance and do some sort of translation from “instance paths” to “XSLT paths”. I’m new to XSLT and have no idea how I could possibly achieve this.

How would I translate such text values into what’s required by the XSLT’s namespace context? Is this even possible? I’m currently thinking about in memory fixes of the XSLT (all this is done within Java) before each validation call – renaming prefixes so that they match the instances binding or injecting new namespace attributes – but this could lead to prefix name clashes and I’m not sure about how it would impact validation performance. I’m open to any suggestion, since I am assuming this is something other people must have come across too (while using Schematron or dyn:evaluate()).

Edit: the clarification of what I’m trying to do follows from here on.

I have an XML instance file which is being edited by a user in the editor. An example of such a file would be:

<?xml version="1.0" encoding="utf-8"?>
<config xmlns="http://example.com/ns/config"
         xmlns:cfg="http://example.com/ns/config"
         xmlns:ns1="http://example.com/ns/custom-01"
         xmlns:ns2="http://example.com/ns/custom-02">
  <ns1:some-element>/cfg:config/ns1:other-element/ns2:nested-element</ns1:some-element>
  <ns1:other-element>
    <ns2:nested-element>some value</ns2:nested-element>
  </ns1:other-element>
</config>

Such a document then goes through schematron validation which is basically an XSLT transformation. It will only be declared valid if the path in ns1:some-element refers to an existing element within the same document (the example above is therefore valid).

The schematron XSLT looks something like this (note that is has been greatly simplified):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--This XSLT was automatically generated from a Schematron schema.-->
<xsl:stylesheet xmlns:iso="http://purl.oclc.org/dsdl/schematron"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dyn="http://exslt.org/dynamic"
                xmlns:exsl="http://exslt.org/common"
                xmlns:sch="http://www.ascc.net/xml/schematron"
                xmlns:cfg="http://example.com/ns/config"
                xmlns:cust1="http://example.com/ns/custom-01"
                xmlns:cust2="http://example.com/ns/custom-02"
                extension-element-prefixes="dyn exsl"
                version="1.0">

<!-- other templates -->

<xsl:template match="/cfg:config/cust1:some-element">
    <xsl:choose>
        <xsl:when test="dyn:evaluate(.)">
            <!-- do stuff -->
        <xsl:when>
    </xsl:choose>
</xsl:template>

<!-- other templates -->

</xsl:stylesheet>

I’m sure this explains the problem. The call to dyn:evaluate(.) will try to evaluate the /cfg:config/ns1:other-element/ns2:nested-element XPath expression, which uses unbound prefixes from the transformation’s prespective (and as such always evaluates to false).

The question was and still is: how can I translate these XPath expressions so that they actually have meaning within the transformation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:52:12+00:00Added an answer on June 1, 2026 at 11:52 am

    XSLT 1 is painful after a decade of XSLT 2:-)

    I believe the stylesheet below should work but it doesn’t in either of the two xslt 1 processors I have available (xsltproc and saxon6) but perhaps it will work in the one you have, or you can use this as a base.

    The idea is simply to replace the prefixes in the source expression by the prefixes used in the stylesheet. xsltproc doesn’t implement the regex module of exlt so I used a simple string replace template (which would fall over prefixes that were substrings of each other, or things that looked like prefixes appearing in strings) however it fails because the xsltproc doesn’t seem to correctly make the string value of a namespace node the namespace: I get

    $ xsltproc cfg.xsl cfg.xml
    xml
    here 1 http://www.w3.org/XML/1998/namespace
    ns2
    here 2
    ns1
    here 2
    cfg
    here 2
    
    
    ===
    /cfg:config/ns1:other-element/ns2:nested-element
    ===
    XPath error : Undefined namespace prefix
    

    so apart from the predefined xml namespace all the namespace nodes have empty string value.

    saxon6 gets this right:
    $ saxon cfg.xml cfg.xsl
    xml
    here 1 http://www.w3.org/XML/1998/namespace
    cfg
    here 1 http://example.com/ns/config
    ns1
    here 1 http://example.com/ns/custom-01
    ns2
    here 1 http://example.com/ns/custom-02
    
    
    ===
    /cfg:config/cust1:other-element/cust2:nested-element
    ===
    
    Error at xsl:choose on line 26 of file:/C:/tmp/cfg.xsl:
      The URI http://exslt.org/dynamic does not identify an external Java class
    Transformation failed: Run-time errors were reported
    

    but doesn’t implement the dyn:evaluate function.

    anyway here’s the code:

    <xsl:stylesheet xmlns:iso="http://purl.oclc.org/dsdl/schematron"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:dyn="http://exslt.org/dynamic"
            xmlns:exsl="http://exslt.org/common"
            xmlns:sch="http://www.ascc.net/xml/schematron"
            xmlns:cfg="http://example.com/ns/config"
            xmlns:cust1="http://example.com/ns/custom-01"
            xmlns:cust2="http://example.com/ns/custom-02"
            extension-element-prefixes="dyn exsl"
            version="1.0">
    
     <!-- other templates -->
    
     <xsl:variable name="ns-catch"><x/></xsl:variable>
     <xsl:variable name="xsl-ns" select="exsl:node-set($ns-catch)/*/namespace::*[name()]"/>
     <xsl:template match="/cfg:config/cust1:some-element">
      <xsl:variable name="fix-source">
       <xsl:call-template name="nsprefix"/>
      </xsl:variable>
      <xsl:message>
    
       ===
       <xsl:value-of select="$fix-source"/>
       ===
      </xsl:message>
      <xsl:choose>
       <xsl:when test="dyn:evaluate($fix-source)">
        <!-- do stuff -->
       </xsl:when>
      </xsl:choose>
     </xsl:template>
    
     <xsl:template name="nsprefix">
      <xsl:param name="source-ns" select="namespace::*[name()]"/>
      <xsl:param name="string" select="."/>
      <xsl:choose>
       <xsl:when test="$source-ns">
        <xsl:message><xsl:value-of select="name($source-ns)"/></xsl:message>
        <xsl:choose>
         <xsl:when test="string($source-ns[1])=$xsl-ns">
          <xsl:message>here 1 <xsl:value-of  select="string($source-ns[1])"/></xsl:message>
          <xsl:variable name="newstring">
           <xsl:call-template name="replace">
        <xsl:with-param name="string" select="$string"/>
        <xsl:with-param name="from" select="concat(name($source-ns[1]),':')"/>
        <xsl:with-param name="to" select="concat(name($xsl-ns[.=$source-ns[1]][1]),':')"/>
           </xsl:call-template>
          </xsl:variable>
          <xsl:call-template name="nsprefix">
           <xsl:with-param name="source-ns" select="$source-ns[position()!=1]"/>
           <xsl:with-param name="string" select="$newstring"/>
          </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
          <xsl:message>here 2</xsl:message>
          <xsl:call-template name="nsprefix">
           <xsl:with-param name="source-ns" select="$source-ns[position()!=1]"/>
           <xsl:with-param name="string" select="$string"/>
          </xsl:call-template>
         </xsl:otherwise>
        </xsl:choose>
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="$string"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
     <xsl:template name="replace">
      <xsl:param name="string"/>
      <xsl:param name="from"/>
      <xsl:param name="to"/>
      <xsl:choose>
       <xsl:when test="contains($string,$from)">
        <xsl:value-of select="substring-before($string,$from)"/>
        <xsl:value-of select="$to"/>
        <xsl:call-template name="replace">
         <xsl:with-param name="string" select="substring-after($string,$from)"/>
         <xsl:with-param name="from" select="$from"/>
         <xsl:with-param name="to" select="$to"/>
        </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="$string"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
     <!-- other templates -->
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.