Page 1 of 1
Using XSLT2 id() Function in Schematron
Posted: Tue Sep 29, 2009 1:04 am
by SoosCreek
I'm having difficulty with what I thought would be a rather simple attempt to use the XQuery/XLST id function to find the correct element associated with a specific id reference. Here's my Schematron attempt to get ANY value at all from the id function:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
<ns uri="MyTargetNamespace" prefix="ns"/>
<title>Simple Sample to Make the XQuery id() function work from within Schematron</title>
<pattern>
<rule context="ns:MyXmlDocument">
<let name="sRef" value="Vehicle/VehicleDriver/@idref"/>
<assert test="0=1">
Driver Reference: |<value-of select="$sRef"/>|
Driver Name: |<value-of select="id($sRef)"/>|
</assert>
</rule>
</pattern>
</schema>
What am I doing wrong? Does oXygen support the id() function? I'll post the .xsd and .xml examples in the next post in case you want to see how it's not working.
Thanks for any advice you can give!
Joel Byford
Re: Using XSLT2 id() Function in Schematron
Posted: Tue Sep 29, 2009 10:25 am
by SoosCreek
Here is the sample XML Schema Document (XSD):
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="MyTargetNamespace"
targetNamespace="MyTargetNamespace">
<xsd:element name="MyXmlDocument" type="XmlDocumentType"/>
<xsd:complexType name="XmlDocumentType">
<xsd:sequence>
<xsd:element name="Person" type="PersonType" maxOccurs="unbounded"/>
<xsd:element name="Vehicle" type="VehicleType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="PersonFirstName" type="xsd:string"/>
<xsd:element name="PersonSurName" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID"/>
</xsd:complexType>
<xsd:complexType name="VehicleType">
<xsd:sequence>
<xsd:element name="VehicleIdentification" type="xsd:string"/>
<xsd:element name="VehicleCommercialIndicator" type="xsd:boolean"/>
<xsd:element name="VehicleDriver" type="VehicleDriverType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="VehicleDriverType">
<xsd:attribute name="idref" type="xsd:IDREF"/>
</xsd:complexType>
</xsd:schema>
Here is a simple instance conformant with that XSD:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<ns:MyXmlDocument xmlns:ns="MyTargetNamespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="MyTargetNamespace file:/C:/Users/Joel%20Byford/Documents/My%20Archives/SimpleSchema.xsd">
<Person id="P1">
<PersonFirstName>John</PersonFirstName>
<PersonSurName>Doe</PersonSurName>
</Person>
<Person id="P2">
<PersonFirstName>Jane</PersonFirstName>
<PersonSurName>Smith</PersonSurName>
</Person>
<Vehicle>
<VehicleIdentification>abc123</VehicleIdentification>
<VehicleCommercialIndicator>true</VehicleCommercialIndicator>
<VehicleDriver idref="P1"/>
</Vehicle>
</ns:MyXmlDocument>
Re: Using XSLT2 id() Function in Schematron
Posted: Tue Sep 29, 2009 12:21 pm
by george
Hi,
In Options->Preferences -- XML -- XML Parser in the ISO Schematron section enable the Use Saxon schema-aware for xslt2 query binding option.
Note that you will need to change also the Schematron file to select something inside the element selected by the ID function, for example I changed that to select all the text nodes like below
Code: Select all
Driver Name: |<value-of select="id($sRef)//text()"/>|
and the result was as below, where you can see that the id function works:
Code: Select all
SystemID: /Users/george/test/test2.xml
Engine name: ISO Schematron (XSLT 2.0)
Severity: error
Description: Driver Reference: |P1| Driver Name: |JohnDoe| (0=1)
Start location: 5:0
Best Regards,
George