代码之家  ›  专栏  ›  技术社区  ›  OMG Ponies

XSLT:选择性节点处理

  •  0
  • OMG Ponies  · 技术社区  · 17 年前

    我有以下xsd代码段:

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

    我一直在尝试使用:

    <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:comment&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> 
    
    1 回复  |  直到 17 年前
        1
  •  0
  •   OMG Ponies    17 年前

    改变:

    <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>