Re: Does anyone have a transform to change comments into annotations in an existing schema?

That fixed it.  I have added some additional code including calling normalize-space() to strip leading and trailing spaces from the comments.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="1.0">
    
    <!--don't preserve any comments that are followed by element declarations-->
    <xsl:template match="
        comment()[following-sibling::node()[not(self::text())][1][self::xs:element]]">
        <!--do nothing with this comment-->
    </xsl:template>
    
    <!--act on element declarations preceded by comments-->
    <xsl:template match="
        xs:element[preceding-sibling::node()[not(self::text())][1][self::comment()]]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xs:annotation>
                <xs:documentation>
                    <p xmlns="http://purl.oclc.org/dsdl/schematron"><xsl:value-of select="normalize-space(preceding-sibling::comment()[1])"/></p>
                </xs:documentation>
            </xs:annotation>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <!--don't preserve any comments that are followed by simpleType declarations-->
    <xsl:template match="
        comment()[following-sibling::node()[not(self::text())][1][self::xs:simpleType]]">
        <!--do nothing with this comment-->
    </xsl:template>
    
    <!--act on simpleType declarations preceded by comments-->
    <xsl:template match="
        xs:simpleType[preceding-sibling::node()[not(self::text())][1][self::comment()]]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xs:annotation>
                <xs:documentation>
                    <p xmlns="http://purl.oclc.org/dsdl/schematron"><xsl:value-of select="normalize-space(preceding-sibling::comment()[1])"/></p>
                </xs:documentation>
            </xs:annotation>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <!--don't preserve any comments that are followed by complexType declarations-->
    <xsl:template match="
        comment()[following-sibling::node()[not(self::text())][1][self::xs:complexType]]">
        <!--do nothing with this comment-->
    </xsl:template>
    
    <!--act on complexType declarations preceded by comments-->
    <xsl:template match="
        xs:complexType[preceding-sibling::node()[not(self::text())][1][self::comment()]]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xs:annotation>
                <xs:documentation>
                    <p xmlns="http://purl.oclc.org/dsdl/schematron"><xsl:value-of select="normalize-space(preceding-sibling::comment()[1])"/></p>
                </xs:documentation>
            </xs:annotation>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@*|node()"><!--identity for all other nodes-->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Received on Wednesday, 21 November 2012 19:11:08 UTC