source: draft-ietf-httpbis/latest/abnf2xml2rfc.xslt @ 454

Last change on this file since 454 was 454, checked in by julian.reschke@…, 14 years ago

fix line wrap in ABNF prose problem; update XSLT for collected ABNF to move diagnostics into separate figure

File size: 3.5 KB
Line 
1<xsl:transform
2  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3  xmlns:xs="http://www.w3.org/2001/XMLSchema"
4  xmlns:x="http://purl.org/net/xml2rfc/ext"
5  version="2.0"
6  exclude-result-prefixes="xs">
7
8<xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
9
10<xsl:param name="abnf"/> 
11 
12<xsl:template match="/">
13  <xsl:variable name="src" select="."/>
14  <xsl:variable name="collected" select="unparsed-text($abnf)"/>
15
16  <xsl:variable name="lines" as="xs:string*" select="tokenize($collected, '\r?\n')"/>
17 
18  <section title="Collected ABNF" anchor="collected.abnf">
19    <xsl:text>&#10;</xsl:text>
20    <figure>
21      <xsl:text>&#10;</xsl:text>
22      <artwork type="abnf" name="{$abnf}">
23        <xsl:for-each select="$lines[substring(.,1,2)!='; ']">
24       
25          <!-- Group by start character -->
26          <xsl:variable name="lineno" select="position()"/>
27          <xsl:variable name="sc1" select="substring(.,1,1)"/>
28          <xsl:variable name="sc0" select="x:laststartchar($lines, $lineno - 1)"/>
29          <xsl:if test="$sc1!=' ' and $sc0!=' ' and $sc1!=$sc0">
30            <xsl:text>&#10;</xsl:text>
31          </xsl:if>
32
33          <!-- Add cross-refs for terms -->
34          <xsl:analyze-string select="." regex='^([A-Za-z0-9\-]+) = ' flags="sm">
35            <xsl:matching-substring>
36              <xsl:variable name="term" select="regex-group(1)"/>
37              <xsl:choose>
38                <xsl:when test="$src//*[@anchor=$term] or $src//x:anchor-alias[@value=$term]">
39                  <x:ref><xsl:value-of select="$term"/></x:ref>
40                </xsl:when>
41                <xsl:otherwise>
42                  <xsl:value-of select="$term"/>
43                </xsl:otherwise>
44              </xsl:choose>
45              <xsl:text> = </xsl:text>
46            </xsl:matching-substring>
47            <xsl:non-matching-substring>
48              <xsl:value-of select="."/>
49            </xsl:non-matching-substring>
50          </xsl:analyze-string>
51
52          <xsl:text>&#10;</xsl:text>
53        </xsl:for-each>
54      </artwork>
55      <xsl:text>&#10;</xsl:text>
56    </figure> 
57    <xsl:text>&#10;</xsl:text>
58   
59    <figure>
60      <preamble>ABNF diagnostics:</preamble>
61      <artwork type="inline">
62        <xsl:for-each select="$lines[substring(.,1,2)='; ']">
63          <xsl:value-of select="."/>
64          <xsl:text>&#10;</xsl:text>
65        </xsl:for-each>
66    </artwork></figure>
67  </section>
68
69  <!-- check whether it's up-to-date... -->
70  <xsl:variable name="src">
71    <xsl:for-each select="//section[@anchor='collected.abnf']//artwork">
72      <xsl:value-of select="."/>
73    </xsl:for-each>
74  </xsl:variable>
75 
76  <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($collected)">
77    <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
78    <!--<xsl:message>A: <xsl:value-of select="//section[@anchor='collected.abnf']//artwork"/></xsl:message>
79    <xsl:message>B: <xsl:value-of select="$collected"/></xsl:message>-->
80  </xsl:if>
81</xsl:template>
82
83<xsl:function name="x:laststartchar">
84  <xsl:param name="lines"/>
85  <xsl:param name="position"/>
86 
87  <xsl:choose>
88    <xsl:when test="' '!=substring($lines[$position],1,1)">
89      <xsl:value-of select="substring($lines[$position],1,1)"/>
90    </xsl:when>
91    <xsl:otherwise>
92      <xsl:value-of select="x:laststartchar($lines, $position - 1)"/>
93    </xsl:otherwise>
94  </xsl:choose>
95</xsl:function>
96 
97 
98</xsl:transform>
Note: See TracBrowser for help on using the repository browser.