2012-10-01 14 views
3

從類型繼承匹配的節點,我們必須定義以下內容的XSD:在XSLT

<xsd:element name="Product" type="cm:Product" abstract="true" /> 
<xsd:complexType name="Product" abstract="true"> 
    <xsd:sequence> 
     <xsd:element name="name" type="xsd:string" minOccurs="0" /> 
     <!-- Other common elements --> 
    </xsd:sequence> 
</xsd:complexType> 

<xsd:element name="Subscription" type="cm:Subscription" /> 
<xsd:complexType name="Subscription"> 
    <xsd:complexContent> 
     <xsd:extension base="cm:Product"> 
      <xsd:sequence> 
       <!-- Subscription specific elements --> 
      </xsd:sequence> 
     </xsd:extension> 
    </xsd:complexContent> 
</xsd:complexType> 

我需要創建一個XSLT這需要產品名稱和一些其他的東西,並把它轉換成一個Web服務請求。問題是,我不知道頂部元素實際上是說cm:Productcm:Subscription還是完全不同的東西(但它擴展了cm:Product)。

有沒有辦法我可以寫一個模板,匹配cm:Product元素和所有元素延伸cm:Product


簡單的例子輸入

<Subscription xmlns="http://schema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <name>Basis</name> 
</Subscription> 

我有什麼到目前爲止

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:cm="http://schema" 
    exclude-result-prefixes="cm"> 

    <xsl:param name="processID" /> 

    <xsl:template match="/"> 
     <xsl:element name="RequestElement"> 
      <xsl:element name="processId"> 
       <xsl:value-of select="$processID" /> 
      </xsl:element> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="cm:Product/cm:name"> 
     <xsl:element name="name"> 
      <xsl:value-of select="." /> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="text()" /> 

</xsl:stylesheet> 

如果我改變cm:Productcm:Subscription,這是我的特殊輸入XML這工作,但問題是,我不知道它實際上是一個釐米:訂閱。所有我「知道」是,它是延長cm:Product

回答

0
<xsl:template match="element(*, cm:Product)/cm:name"> 

應該做的一個元素,但你需要添加合適的頂級

<xsl:import-schema namespace="https://schema" schema-location="....." /> 

,當然這需要你處理器是架構感知的。

+0

似乎這樣做會做到這一點,如果沒有得到這個錯誤消息,而不是:*要使用xsl:導入架構,你需要架構感知版本的撒克遜*:/哦... ... – Svish

+0

如果你'確保你的輸入都是單一元素,你可以用'match =「*/cm:name」''' –

+0

這樣的類型就可以逃脫。這將確實有效。 – Svish