XSLT: Copy child elements of a complex type only o

2020-05-03 11:11发布

I have the following input structure:

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

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

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.

Thanx for any help!

Edit:

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>

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.

标签: xslt
1条回答
我只想做你的唯一
2楼-- · 2020-05-03 11:29

Instead of

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

use

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

or define a key

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

and then you can use

<xsl:copy-of select="key('complex-types', xs:element/@type)"/>
查看更多
登录 后发表回答