2012-05-07 1 views
1

Relax NG XML 스키마의 어노테이션에서 매우 간단한 문서를 생성하려고합니다. 같은 유효한 XML 파일에 사용되는Relax NG 스키마에서 기본 문서를 생성하는 방법

<?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 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를 나열 문서를 생성 할 수 있도록하고 싶습니다 예를 들어, 휴식을 취 다음 NG를 부여 각 요소/속성을 정의한 다음 해당 문서 주석을 표시합니다. 예를 들어 :

/topNode 
This is the top of the doc. 

/topNode/level1 
Here's some notes about level1 

/topNode/level1/@att1 
Details about att1. 

etc... 

은 결국, 난 등 "zeroOrMore"가능한 데이터 유형에 대한 자세한 문서에 추가 할 것입니다 ...하지만 내가 먼저 해결이 첫 걸음을 얻을 필요가있다.

나는 Techquila RELAX-NG Documentation Tools을 찾았습니다. 내가 docbook 스타일 시트에 rng 함께 놀았지만, 내가 찾고있는 일을하지 않습니다. 내가 말할 수있는 한 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

멋지게 작동합니다. 감사. –