【发布时间】:2009-07-08 18:28:13
【问题描述】:
使用this file 作为源,我需要从本地源文件或导入中注明的相关文件中检索元素。 type 值使用冒号分隔两个值 - substring-before(@type, ':') 告诉我要引用哪个文件; substring-after(@type, ':') 是我需要以相同方式复制和迭代其内容的文件中元素的名称。
示例:我想要名称为“PersonType”的 xs:complexType,因此我使用副本来获取它及其子项。下一步是查看那些子元素——对于那些是 xs:element 的子元素,我想检索类型值 ("AcRec:HighSchoolType") 中引用的元素。 “AcRec”告诉我需要使用哪个 xsd,所以我知道我会在该 xsd 中找到名称值为“HighSchoolType”的东西。查看 AcRec xsd,我知道“HighSchoolType”是一个 xs:complexType(我已经定义了要处理的模板),所以我应该看到输出。
<?xml version="1.0" encoding="ISO-8859-1"?>
<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="/">
<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">
<xsl:apply-templates select="//xs:complexType[@name='PersonType']" />
</xs:schema>
</xsl:template>
<xsl:template match="xs:complexType">
<xsl:copy>
<xsl:copy-of select="node()[not(xs:annotation | xs:restriction)]|@*"/>
</xsl:copy>
<xsl:apply-templates select=".//xs:element" />
</xsl:template>
<xsl:template match="xs:simpleType">
<xsl:copy>
<xsl:copy-of select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xs:element">
<xsl:choose>
<xsl:when test="substring-before(@type, ':') = 'AcRec'">
<xsl:text>AcRec</xsl:text>
<xsl:apply-templates select="document('[local file path]AcademicRecord_v1.3.0.xsd')//*[@name=substring-after(@type, ':')]" />
</xsl:when>
</xsl:choose>
</xsl:template>
所需的输出如下所示:
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="HighSchool" type="AcRec:HighSchoolType" minOccurs="0">
</xs:sequence>
</xs:complexType>
<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:when 时,我在查看文档时遗漏了什么? xsl:text 告诉我我在,但后续行没有返回任何输出。
此外,如何在复制 xs:complextType 和 xs:simpleType 元素时排除 xs:annotation 和 xs:restriction 元素的出现?我无法让 dpawson 网站上提到的示例正常工作。
【问题讨论】:
标签: xslt