phax / ph-schematron

Java Schematron library that supports XSLT and native application
Apache License 2.0
111 stars 36 forks source link

Namespace in output #90

Closed robrechtD closed 4 years ago

robrechtD commented 4 years ago

We are currently on version 4.3.4 of the schematron validator. The output of the validation includes the namespaces however like below. /:Invoice[namespace-uri()='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'][1]/:InvoiceLine[namespace-uri()='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'][1]/:Price[namespace-uri()='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'][1]/:BaseQuantity[namespace-uri()='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'][1]

Is there a way to exclude the "[namespace-uri()='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'][1] " from the output? By updating our version or adapting the schematron?

phax commented 4 years ago

@robrechtD I suggest you switch to the latets 5.4.0 version - should be no big deal I hope....

Basically it is possible by implementing the "SPI interface" ISVRLLocationBeautifierSPI. It has one method: @Nullable String getReplacementText (@Nonnull String sNamespaceURI, @Nonnull String sLocalName);

A custom implementation I am using in a different project is use a central map:

public class LocationBeautifierSPI implements ISVRLLocationBeautifierSPI
{
  private static final SimpleReadWriteLock s_aRWLock = new SimpleReadWriteLock ();
  @GuardedBy ("s_aRWLock")
  private static final MapBasedNamespaceContext s_aCtx = new MapBasedNamespaceContext ();

  public static void addMapping (@Nonnull final String sPrefix, @Nonnull final String sNamespaceURI)
  {
    // Allow overwrite!
    s_aRWLock.writeLocked ( () -> s_aCtx.setMapping (sPrefix, sNamespaceURI));
  }

  public static void addMappings (@Nullable final IIterableNamespaceContext aOther)
  {
    // Allow overwrite!
    if (aOther != null)
      s_aRWLock.writeLocked ( () -> s_aCtx.setMappings (aOther));
  }

  @Nonnull
  public static EChange removeAllMappings ()
  {
    return s_aRWLock.writeLocked (s_aCtx::clear);
  }

  @Deprecated
  @UsedViaReflection
  public LocationBeautifierSPI ()
  {}

  @Nullable
  public String getReplacementText (@Nonnull final String sNamespaceURI, @Nonnull final String sLocalName)
  {
    final String sPrefix = s_aRWLock.readLocked ( () -> s_aCtx.getPrefix (sNamespaceURI));
    if (sPrefix == null)
      return null;

    return sPrefix + ":" + sLocalName;
  }
}

Don't forget to add the respective META-INF\services\com.helger.schematron.svrl.ISVRLLocationBeautifierSPI file....

hth

robrechtD commented 4 years ago

Hi Philip

Thanks for your help, I ended up upgrading and implementing my own function. But your code served as a nice example.

Kind regards