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

At 2012-11-21 13:10 -0600, Loren Cahlander wrote:
>That fixed it.  I have added some additional code including calling 
>normalize-space() to strip leading and trailing spaces from the comments.

Because of the repetition you have, you may find the (not yet checked 
for typos) version of your stylesheet below will serve you better 
from a maintenance perspective.  With this stylesheet any common 
fixes you need can be done in one place, not three.

Note that the indentation of the <xsl:value-of> in your stylesheet 
does not introduce indentation in your result, so there is no need 
for you to stretch out your content so long on one line.  XSLT works 
on nodes, not on angle brackets, and the white-space between nodes in 
a stylesheet are ignored.

I hope this helps.

. . . . . . . . Ken

<?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 certain declarations-->
   <xsl:template
     match="comment()[following-sibling::node()[not(self::text())][1]
            [self::xs:element or self::xs:simpleType or 
self::xs:complexType]]">
     <!--do nothing with this comment-->
   </xsl:template>

   <!--act on certain declarations preceded by comments-->
   <xsl:template match="
  xs:element[preceding-sibling::node()[not(self::text())][1][self::comment()]]
|
  xs:simpleType[preceding-sibling::node()[not(self::text())][1][self::comment()]]
|
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>

--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/x/
G. Ken Holman                   mailto:gkholman@CraneSoftwrights.com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

Received on Wednesday, 21 November 2012 20:01:17 UTC