XSLT:使用命名模板将XSD复制到XSD

XSLT:使用命名模板将XSD复制到XSD,xslt,Xslt,我有XSD的内容 <xsl:template match="node()|@*" name="copy"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="xs:complexType"> <xsl:call-templa

我有XSD的内容

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
使用此xsl,我可以复制所需元素的内容:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>

核心
阿克雷克

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
其思想是在xsd中选择一个特定的节点,并通过基于类型值查找来将所有支持元素生成到单个文件中

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
所需的输出最终应如下所示:

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:core="urn:org:pesc:core:CoreMain:v1.2.0" xmlns:AcRec="urn:org:pesc:sector:AcademicRecord:v1.1.0">
<xs:complexType name="PersonType">
    <xs:sequence>
        <xs:element name="SchoolAssignedPersonID" type="core:SchoolAssignedPersonIDType" minOccurs="0"/>
        <xs:element name="SIN" type="core:SINIDType" minOccurs="0"/>
        <xs:element name="NSN" type="core:NSNIDType" minOccurs="0"/>
        <xs:element name="AgencyAssignedID" type="core:AgencyAssignedIDType" minOccurs="0"/>
        <xs:element name="RecipientAssignedID" type="core:RecipientAssignedIDType" minOccurs="0"/>
        <xs:element name="SSN" type="core:SSNType" minOccurs="0"/>
        <xs:element name="Birth" type="core:BirthType" minOccurs="0"/>
        <xs:element name="Name" type="core:NameType"/>
        <xs:element name="AlternateName" type="core:NameType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="HighSchool" type="AcRec:HighSchoolType" minOccurs="0" />
        <xs:element name="Contacts" type="AcRec:ContactsType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="Gender" type="core:GenderType" minOccurs="0"/>
        <xs:element name="Residency" type="AcRec:ResidencyType" minOccurs="0"/>
        <xs:element name="Deceased" type="core:DeceasedType" minOccurs="0"/>
        <xs:element name="NoteMessage" type="core:NoteMessageType" minOccurs="0" maxOccurs="unbounded"/>        
    </xs:sequence>
</xs:complexType>
<xs:simpleType name="SchoolAssignedPersonIDType" />
<xs:simpleType name="SINIDType" />
<xs:simpleType name="NSNIDType" />
<xs:simpleType name="AgencyAssignedIDType" />
<xs:complexType name="HighSchoolType">
    <xs:sequence>
        <xs:element name="OrganizationName" type="core:OrganizationNameType"/>
        <xs:element name="OPEID" type="core:OPEIDType"/>
        <xs:element name="NCHELPID" type="core:NCHELPIDType"/>
        <xs:element name="IPEDS" type="core:IPEDSType"/>
        <xs:element name="ATP" type="core:ATPType"/>
        <xs:element name="FICE" type="core:FICEType"/>
        <xs:element name="ACT" type="core:ACTType"/>
        <xs:element name="CCD" type="core:CCDType"/>
        <xs:element name="CEEBACT" type="core:CEEBACTType"/>
        <xs:element name="CSIS" type="core:CSISType"/>
        <xs:element name="USIS" type="core:USISType"/>
        <xs:element name="ESIS" type="core:ESISType"/>
        <xs:element name="DUNS" type="core:DUNSType"/>
    </xs:sequence>
</xs:complexType>
<xs:simpleType name="OrganizationNameType" />
<xs:simpleType name="OPEIDType" />

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
我的想法是使用模板的组合——首先复制以获取元素和子元素(排除xs:annotations和xs:restrictions)。第二个模板将迭代元素的xs:element内容,以从另一个文件检索引用。此外,当遇到xs:groups时,xs:groupref元素将替换为xs:groupdefinition中元素的内容。

您不能“强制”它使用具有
匹配属性的模板。此类模板将应用于与之匹配的所有元素,并且仅应用于这些元素。因此,如果与
node()
匹配的一些子元素是
complexType
,它将自动应用

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
当然,您可以给它一个
名称
,然后使用您可以使用的

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
<xsl:apply-templates select="complexType"/>
<xsl:apply-templates select="node()[local-name() != 'complexType']|@*"/>

这将在其余节点之前输出所有复杂节点。或者,要更改顺序,您需要执行以下操作:

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
<xsl:apply-templates select="node()[local-name() != 'complexType']|@*"/>
<xsl:apply-templates select="complexType"/>

这有点像是瞎猜,但我认为您在XSL样式表中缺少对名称空间的正确处理

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
您创建的“
complexType
”模板与“
xs:complexType
”节点不匹配。必须声明
xs
命名空间,如下所示:

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

你也必须使用它:

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
<xsl:template match="xs:complexType">
  <!-- ... -->
</xsl:template>

<xsl:template match="xs:element">
  <!-- ... -->
</xsl:template>

一旦我做了更改,您的样式表给了我:

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="PersonType">
    <xs:sequence>core core core core core core core core core AcRec AcRec core AcRec core core</xs:sequence>
  </xs:complexType>
</xs:schema>

芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯芯
这清楚地表明模板正在被使用。XSL总是使用它能为任何给定节点找到的最具体的模板,就像CSS使用它能找到的最具体的规则一样

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
具体得多,但除非使用
xs
名称空间,否则后者与任何内容都不匹配

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
附言:几句话:

<xsl:template match="node()|@*" name="copy">
    <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:call-template name="copy"/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:choose>
        <xsl:when test="contains(@type, 'core')">
            core
        </xsl:when>
        <xsl:when test="contains(@type, 'AcRec')">
            AcRec
        </xsl:when>         
    </xsl:choose>
</xsl:template>
  • 不是强制性的,如果它为空,则可以将其省略
  • 无需使用
    。一旦您定义了希望看到的输出,我们就可以使用更干净的方法来摆脱它
  • 作为您的
    测试,我建议在
    包含(@type,'core')
    之前使用
    子字符串(@type,'core')
    。后者更容易出错
  • 这可能只是示例的一部分,但您应该使用
    core
    ,以避免输出中出现不必要的空白

不知何故,你忘了提及你正在努力实现的目标。你能至少发布一下想要的输出吗?谢谢-我一直在努力解决这个问题,非常感谢你的帮助。