Page 1 of 1

Schematron: add attribute value to XREF

Posted: Tue Jan 21, 2020 1:53 pm
by pieterjan_vdw
Hi,

All my xref elements with the @format 'dita' require a @platform attribute (value mechanical or terminal).
If the @platform element is not added, I would like to add a quickfix to add the attibute value.

My search is currently working, but the quickfix is not working yet.
The attribute value is not added the XREF element.
Anyone who can help me to add the attribute value.

This is my schematron:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://purl.oclc.org/dsdl/schematron" xmlns:sqf="http://www.schematron-quickfix.com/validator/process" queryBinding="xslt2">
	<pattern abstract="true" id="requiredAttribute">
		<title>Required Attributes</title>
		<rule context="$context">
			<assert test="$attribute= $terminal or $mechanical" sqf:fix="add_attribute_mechanical add_attribute_display">
				The <name/> element should have a
				<value-of select="$attribute/name()"/> attribute.
			</assert>
			<sqf:fix id="add_attribute_mechanical">
				<sqf:description>
					<sqf:title>Adds the value 'mechanical' to the @plaform attribute</sqf:title>
				</sqf:description>
				<sqf:add  match="." node-type="attribute" target="platform">
					<value-of select=".">mechanical</value-of>
				</sqf:add>
			</sqf:fix>
			<sqf:fix id="add_attribute_display">
				<sqf:description>
					<sqf:title>Adds the value 'terminal' to the @platform attribute</sqf:title>
				</sqf:description>
				<sqf:add  match="." node-type="attribute" target="platform">
					<value-of select=".">terminal</value-of>
				</sqf:add>
			</sqf:fix>
		</rule>
	</pattern>
	<pattern is-a="requiredAttribute">
		<param name="context" value="xref[@format='dita']"/>
		<param name="attribute" value="@platform"/>
		<param name="terminal" value="terminal"/>
		<param name="mechanical" value="mechanical"/>
	</pattern>
</schema>

Re: Schematron: add attribute value to XREF

Posted: Tue Jan 21, 2020 3:05 pm
by Radu
Hi Pieterjan,

I would suggest these changes:

Code: Select all

<schema xmlns="http://purl.oclc.org/dsdl/schematron" xmlns:sqf="http://www.schematron-quickfix.com/validator/process" queryBinding="xslt2">
    <pattern abstract="true" id="requiredAttribute">
        <title>Required Attributes</title>
        <rule context="$context">
            <assert test="$attribute = $terminal or $attribute = $mechanical" sqf:fix="add_attribute_mechanical add_attribute_display">
                The <name/> element should have a
                <value-of select="$attrName"/> attribute.
            </assert>
            <sqf:fix id="add_attribute_mechanical">
                <sqf:description>
                    <sqf:title>Adds the value <value-of select="$mechanical"/> to the @<value-of select="$attrName"/> attribute</sqf:title>
                </sqf:description>
                <sqf:add  match="." node-type="attribute" target="platform">
                    <value-of select="$mechanical"/>
                </sqf:add>
            </sqf:fix>
            <sqf:fix id="add_attribute_display">
                <sqf:description>
                    <sqf:title>Adds the value <value-of select="$terminal"/> to the @<value-of select="$attrName"/> attribute</sqf:title>
                </sqf:description>
                <sqf:add  match="." node-type="attribute" target="platform">
                    <value-of select="$terminal"/>
                </sqf:add>
            </sqf:fix>
        </rule>
    </pattern>
    <pattern is-a="requiredAttribute">
        <param name="context" value="xref[@format='dita' or not(@format)][not(@platform)]"/>
        <param name="attribute" value="@platform"/>
        <param name="attrName" value="'platform'"/>
        <param name="terminal" value="'terminal'"/>
        <param name="mechanical" value="'mechanical'"/>
    </pattern>
</schema>
I also assumed that you want to also match xref's which do not have the @format attribute (because by default if the format attribute is missing, then it is interpreted as a reference to a DITA topic). Also I restricted the assert to not match <xref> elements which already have the @platform attribute set on them.

Regards,
Radu