XSLT:仅复制复杂类型的子元素一次 [英] XSLT: Copy child elements of a complex type only once

查看:14
本文介绍了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)也作为复杂类型包含在该文档中(除了许多其他类型).

All the referenced types (AdressType,PartnerType,DateType) are also contained in that document as complex types (besides many other).

我想做的是复制InvoiceType"中使用的类型到一个新文档.我的问题是,AdressType 在 InvoiceType 中使用了不止一次,因此它在我的新文档中出现了不止一次.我怎样才能防止这种情况?我需要诸如如果已经处理过 -> 跳过"之类的内容,但这不是声明性的...也许 xslt 不是实现此目的的正确方法.

What i am trying to do is to copy the types that are used within a "InvoiceType" to a new document. My Problem is, the AdressType is used more then once within InvoiceType, and thus it appears more then once in my new document. How can i prevent that? I need something like "if that is already pocessed ->skip" but that is not declarativ... maybe xslt is not the right way to achive this.

感谢您的帮助!

我目前使用的 XSLT 看起来像这样(修改为取悦简单示例)

My XSLT i am using so far looks like that (modified to please the simple example)

<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 应用该模板.基本上我会抛出它的内容,查看引用了哪些类型,通过它们的名称在文档中查找它们并复制它们.

I am applying that template for the InvoceType. Basically i am going threw its contents, see what types are referenced, look them up in the document via their name and copy them.

推荐答案

代替

<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)"/>

这篇关于XSLT:仅复制复杂类型的子元素一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆