2012-05-07 25 views
1

我試圖從Relax NG XML Schema中的註釋生成非常簡單的文檔。例如,給出下面的RELAX NG:如何從Relax NG Schema生成基本文檔

<?xml version="1.0" encoding="UTF-8"?> 
<grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> 
    <start> 
     <element name="topNode"> 
      <ref name="topNode-ref"/> 
     </element> 
    </start> 

    <define name="topNode-ref"> 
     <a:documentation>This is the top of the doc.</a:documentation> 
     <oneOrMore> 
      <element name="level1"> 
       <ref name="level1-ref"/> 
      </element> 
     </oneOrMore> 
    </define> 

    <define name="level1-ref"> 
     <a:documentation>Here's some notes about level1.</a:documentation> 
     <attribute name="att1"> 
      <a:documentation>Details about att1.</a:documentation> 
     </attribute> 
     <element name="subLevel2"> 
      <ref name="subLevel2-ref"/> 
     </element> 
    </define> 

    <define name="subLevel2-ref"> 
     <a:documentation>Notes on subLevel2.</a:documentation> 
     <attribute name="subAtt"/> 
     <zeroOrMore> 
      <element name="subLevel3"> 
       <ref name="subLevel3-ref"/> 
      </element> 
     </zeroOrMore> 
    </define> 

    <define name="subLevel3-ref"> 
     <a:documentation>And here is subLevel3.</a:documentation> 
     <attribute name="subSubAtt"/> 
    </define> 
</grammar> 

這將被用來有效的XML文件,如:

<?xml version="1.0" encoding="UTF-8"?> 
<topNode> 
    <level1 att1="some test"> 
     <subLevel2 subAtt="more text"></subLevel2> 
    </level1> 

    <level1 att1="quick"> 
     <subLevel2 subAtt="brown"> 
      <subLevel3 subSubAtt="fox"></subLevel3> 
     </subLevel2> 
    </level1> 
</topNode> 

我希望能夠產生的文檔,其中列出了基本的XPath每個元素/屬性,然後顯示任何相應的文檔註釋。例如:

/topNode 
This is the top of the doc. 

/topNode/level1 
Here's some notes about level1 

/topNode/level1/@att1 
Details about att1. 

etc... 

最後,我將添加約「零次或多次」,可能的數據類型等多種文檔中......但我需要首先解決的第一步。我找到Techquila RELAX-NG Documentation Tools。我玩過docbook樣式表,但它沒有做我正在尋找的東西。就我所知,它只是單獨列出了沒有關於XPath細節的元素。我不明白我可以如何使用它作爲獲取輸出結果的起點。

假設提供了RelaxNG示例,是否有可能(如果是這樣,如何?)用XSLT生成這種類型的文檔輸出?

雖然XSLT將是理想的,但這不是必需的。我願意接受任何能夠完成工作的事情。

回答

2

這將適用於像您的示例一樣非常簡單的語法。

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:r="http://relaxng.org/ns/structure/1.0" 
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" 
> 
<xsl:output method="text" /> 

<xsl:template match="/"> 
    <xsl:apply-templates select="//r:define[a:documentation] | //r:attribute[a:documentation]" /> 
</xsl:template> 

<xsl:template match="r:define"> 
    <xsl:variable name="doc" select="a:documentation" /> 
    <xsl:call-template name="print-path"> 
     <xsl:with-param name="elm" select="//r:element[r:ref/@name=current()/@name]" /> 
    </xsl:call-template> 
    <xsl:value-of select="$doc" /><xsl:text>&#10;</xsl:text> 
</xsl:template> 

<xsl:template match="r:attribute"> 
    <xsl:variable name="doc" select="a:documentation" /> 
    <xsl:call-template name="print-path"> 
     <xsl:with-param name="elm" select="//r:element[r:ref/@name=current()/ancestor::r:define/@name]" /> 
     <xsl:with-param name="path" select="concat('/@',@name)" /> 
    </xsl:call-template> 
    <xsl:value-of select="$doc" /><xsl:text>&#10;</xsl:text> 
</xsl:template> 

<xsl:template name="print-path"> 
    <xsl:param name="elm" /> 
    <xsl:param name="path" /> 

    <xsl:variable name="parent" select="//r:ref[@name=$elm/ancestor::r:define/@name]/ancestor::r:element" /> 
    <xsl:message><xsl:value-of select="$elm/@name" /></xsl:message> 
    <xsl:choose> 
     <xsl:when test="$parent"> 
      <xsl:call-template name="print-path"> 
       <xsl:with-param name="elm" select="$parent" /> 
       <xsl:with-param name="path" select="concat('/',$elm/@name,$path)" /> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="concat('/',$elm/@name,$path)" /><xsl:text>&#10;</xsl:text>    
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 
+0

奇妙地工作。謝謝。 –