2009-07-09 56 views
0

我有以下XSD片段:XSLT:選擇性節點處理

<xs:complexType name="HighSchoolType"> 
    <xs:sequence> 
     <xs:element name="OrganizationName" type="core:OrganizationNameType"/> 
     <xs:group ref="core:OrganizationIDGroup" minOccurs="0"/> 
    </xs:sequence> 
</xs:complexType> 

我想用來處理xs:元素標籤不同於XS:組標籤,而完全無視XS:註釋和xs:限制標記。當我有:

  • xs:element tag,我想複製它。
  • xs:組標籤,我希望輸出包含xs:group標籤的子標籤。
  • 其它任何標籤可以忽略不計,我不希望它在我的輸出

我一直在嘗試使用:

<xsl:template match="xs:complexType"> 
    <xsl:copy> 
     <xsl:choose> 
      <xsl:when test="*[self::xs:element]|@*"> 
       <xsl:copy-of select="."/> 
      </xsl:when> 
      <xsl:when test="*[self::xs:group]|@*"> 
       <xsl:apply-templates select="."/> 
      </xsl:when>    
     </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

我不明白爲什麼這樣的:

<xsl:copy-of select="*[not(self::xs:annotation or self::xs:restriction)]|@*"/> 

...將排除XS:註釋& XS:限制節點而

<xsl:when test="*[self::xs:element]|@*"> 
    <xsl:copy-of select="."/> 
</xsl:when> 

...返回一切,而:

<xsl:when test="*[self::xs:group]|@*"> 
    <xsl:apply-templates select="."/> 
</xsl:when> 

...從來沒有觸發:

<xsl:variable name="core" select="document('CoreMain_v1.4.0.xsd')" /> 
<xsl:variable name="AcRec" select="document('AcademicRecord_v1.3.0.xsd')" /> 

<xsl:template match="xs:group[@ref]"> 
    <xsl:variable name="name" select="substring-after(@ref, ':')" /> 

    <xsl:choose> 
     <xsl:when test="substring-before(@ref, ':') = 'AcRec'">    
      <xsl:apply-templates select="$AcRec//*[@name=$name]" /> 
     </xsl:when> 
     <xsl:when test="substring-before(@ref, ':') = 'core'">    
      <xsl:apply-templates select="$core//*[@name=$name]" /> 
     </xsl:when>    
    </xsl:choose> 
</xsl:template> 

<xsl:template match="xs:group[@name]"> 
    <xsl:copy-of select="node()[not(self::xs:annotation|self::xs:restriction)]|@*"/> 
</xsl:template> 

回答

0

更改:

<xsl:template match="xs:complexType"> 
    <xsl:copy> 
      <xsl:choose> 
        <xsl:when test="*[self::xs:element]|@*"> 
          <xsl:copy-of select="."/> 
        </xsl:when> 
        <xsl:when test="*[self::xs:group]|@*"> 
          <xsl:apply-templates select="."/> 
        </xsl:when>        
      </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

...到:

<xsl:template match="xs:complexType"> 
    <xsl:copy> 
    <xsl:for-each select="@*"> 
     <xsl:copy /> 
    </xsl:for-each> 
    <xsl:apply-templates select="node()" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="xs:sequence"> 
    <xsl:copy> 
    <xsl:for-each select="node()"> 
     <xsl:choose> 
     <xsl:when test="self::xs:element"> 
      <xsl:copy-of select="." /> 
     </xsl:when> 
     <xsl:when test="self::xs:group"> 
      <xsl:apply-templates select="."/> 
     </xsl:when>     
     </xsl:choose> 
    </xsl:for-each> 
    </xsl:copy> 
</xsl:template>