XSLT:只复制一次复杂类型的子元素

时间:2017-11-10 11:29:25

标签: xslt

我有以下输入结构:

<complexType name="InvoiceType">
  <xs:element name="AdressIn"      type="AdressType"/>
  <xs:element name="AdressOut"     type="AdressType" />
  <xs:element name="Partner"       type="PartnerType" />
  <xs:element name="Date"          type="DateType"/>
</complexType>

所有引用的类型(AdressType,PartnerType,DateType)也作为复杂类型(除了许多其他类型)包含在该文档中。

我要做的是复制“InvoiceType”中使用的类型 到一个新文件。 我的问题是,AdressType在InvoiceType中使用了一次以上,因此它在我的新文档中出现的时间超过一次。我怎么能防止这种情况? 我需要一些类似“如果那已经被处理过了 - &gt;跳过”但这不是声明...也许xslt不是实现这一目标的正确方法。

Thanx任何帮助!

编辑:

我到目前为止使用的XSLT看起来像那样(为了取悦简单的例子而修改)

<xsl:template match="xs:complexType"> 
<xsl:for-each select="./xs:element">      
  <xsl:variable name="elementType"><xsl:value-of select="@type"></xsl:value-of></xsl:variable>
  <xsl:copy-of copy-namespaces="no" select="/xs:schema/xs:complexType[@name=$elementType]"/>
</xsl:for-each>

我正在为InvoceType应用该模板。基本上我要扔掉它的内容,看看引用了哪些类型,通过它们的名称在文档中查找它们并复制它们。

1 个答案:

答案 0 :(得分:1)

而不是

<xsl:for-each select="./xs:element">      
  <xsl:variable name="elementType"><xsl:value-of select="@type"></xsl:value-of></xsl:variable>
  <xsl:copy-of copy-namespaces="no" select="/xs:schema/xs:complexType[@name=$elementType]"/>
</xsl:for-each>

使用

<xsl:copy-of select="/xs:schema/xs:complexType[@name=current()/xs:element/@type]"/>

或定义一个键

<xsl:key name="complex-types" match="xs:schema/xs:complexType" use="@name"/>

然后你可以使用

<xsl:copy-of select="key('complex-types', xs:element/@type)"/>