2012-05-14 40 views
1

我有一個XSD文檔,我試圖用XSL解析文檔用途,其中complexTypes經常包含其他complexTypes的元素。如果可能,我想在容器旁邊顯示這些複雜類型元素的內容。這裏是我工作的一個簡單的例子:使用XSL擴展XSD複雜元素類型?

<xs:complexType name="S"> 
    <xs:sequence> 
    <xs:element name="A" type="X"/> 
    </xs:sequence> 
</xs:complexType> 

<xs:complexType name="X"> 
    <xs:sequence> 
    <xs:element name="F"/> 
    <xs:element name="G"/> 
    <xs:element name="H"/> 
    </xs:sequence> 
</xs:complexType> 

我將如何得到上面顯示的是這樣的:「S包含X型的(包含F,G和H)」?

感謝您提前提供任何幫助!

新增例如:

<xsl:for-each select="*"> 
    <xsl:choose> 

     <!-- call template without param --> 
     <xsl:when test="name() = 'xs:complexType'"> 
     <xsl:value-of select="@name"/> 
     <xsl:text> contains </xsl:text> 

     <xsl:call-template name="top"/> 
     <xsl:text>&#xa;</xsl:text> 
     </xsl:when> 

     <!-- for contained elements with types --> 
     <xsl:when test="@type != '' and name() = 'xs:element' and $typeToLocate = ''"> 
     <xsl:value-of select="@name"/> 
     <xsl:text> of type </xsl:text> 
     <xsl:value-of select="@type"/> 
     <xsl:text>(which contains: </xsl:text> 

     <!-- 
      point at which i want processor to return to root, locate the 
      indicated complex type, output its contents then continue going 
      through schema. 
     --> 
     <xsl:call-template name="top"> 
      <xsl:with-param name="typeToLocate" select="@type"/> 
     </xsl:call-template> 

     <xsl:text>)</xsl:text> 
     </xsl:when> 

     <!-- when type is located, send it to signal proper output --> 
     <xsl:when test="$typeToLocate != '' and $typeToLocate = @name"> 
     <xsl:call-template name="top"> 
      <xsl:with-param name="typeToLocate" select="$typeToLocate"/> 
     </xsl:call-template> 
     </xsl:when> 

     <!-- for elements contained in indicated type --> 
     <xsl:when test="$typeToLocate != '' and name() = 'xs:element'"> 
     <xsl:value-of select="@name"/> 
     <xsl:text> </xsl:text> 

     <xsl:call-template name="top"> 
      <xsl:with-param name="typeToLocate" select="$typeToLocate"/> 
     </xsl:call-template> 
     </xsl:when> 

     <!-- for continuing through non-content elements under found type --> 
     <xsl:when test="$typeToLocate != ''"> 
     <xsl:call-template name="top"> 
      <xsl:with-param name="typeToLocate" select="$typeToLocate"/> 
     </xsl:call-template> 
     </xsl:when> 

     <!-- for ignoring non-content elements during normal processing --> 
     <xsl:otherwise> 
     <xsl:call-template name="top"/> 
     </xsl:otherwise> 

    </xsl:choose> 
    </xsl:for-each> 

</xsl:template> 
+0

爲了擴大Garrison的迴應,缺少爲您編寫代碼(我不這樣做,這不是一個免費的編程服務),不知道你卡在哪裏就很難回答這個問題。向我們展示您在解決方案上的最佳嘗試將揭示您知道的以及您不知道的內容,這將有助於我們提出可幫助您的答案。 –

+0

對不起,添加了我已經寫過的部分(會更快地附加它,但它不太可讀)的編輯。希望它傳達我正在尋找的東西。 – erodious

+0

只是好奇,這是爲了測量XSLT/XSD技能的某種任務嗎?或者一個有趣的項目爲一個特定的XSD?我問的原因......如果你使用XML命名空間怎麼辦?你打算將前綴擴展到URI嗎?如果XSD引用其他XSD(包括/導入/重新定義)會怎麼樣?爲了好玩,嘗試編寫一些循環依賴關係:類型,組或XSD文件...我的意思是,如果您使用的是現實生活中的XSD,則可能需要重新考慮對此特定任務使用XSLT。 –

回答

0

籠統地回答你的問題,但未有提供具體樣式表,我建議......

(1)創建一個模板來匹配複雜類型在XSD公共層面。所述模板的序列構造器將直接負責生成諸如「S」之類的文本。

(2)在所述序列構造函數中,但在所述文本生成之後,調用複雜類型節點。這將負責所需生產的剩餘部分,從「包含...」開始。

(3)創建描述內容模板。這裏的序列構造器將由兩部分組成: (3.1)像「包含」一樣的持續生產; (3.2)調用序列成員的apply-templates。 (4)創建一個匹配xs:sequence/xs:元素的模板。從3.2開始你會猜到,序列構造函數將包含3個部分: (4.1)類似於X的類型(其中「 (4.2)調用與子複雜類型相關的describe-contents模板) ;最後是 (4.3)「)」的常量產生

這應該這樣做,如果你的目標僅限於xs:序列和複雜類型。決不-的少,這種基於模板的方法,其中模板匹配大致到語法元素,應該是可擴展到XS:選擇等

我假設你真正想要的深層次複合型的遞歸描述定義。