user2539823
user2539823

Reputation: 103

Remove references from schema xsd

Does anybody know, how to transform xsd String contains references to String which haven't the 'ref' attribute?

For example I have schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pref="http://someaddr.com" elementFormDefault="qualified" targetNamespace="http://otheraddr.com">
   <xs:element name="rootElem">
     <xs:complexType>
       <xs:sequence>
         <xs:element ref="pref:elem1"/>
         <xs:element ref="pref:elem2"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
   <xs:element name="elem1" type="xs:string"/>
   <xs:element name="elem2" type="xs:string"/>
</xs:schema>

and I want to transform that to:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pref="http://someaddr.com" elementFormDefault="qualified" targetNamespace="http://otheraddr.com">
   <xs:element name="rootElem">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="elem1" type="xs:string"/>
         <xs:element name="elem2" type="xs:string"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
</xs:schema>

I looking for something function for example xerces libs, but I can't found anything. Also writing it on my own in Java makes many difficulties.

Upvotes: 0

Views: 875

Answers (1)

John Bollinger
John Bollinger

Reputation: 180048

An XSLT stylesheet describing this transformation might look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    >

  <!-- transform all attributes and nodes as themselves,
       except where a more selective rule applies -->
  <xsl:template match="@*|node()" mode="#all">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <!-- transform xs:element elements with ref attributes
       as a copy of the referenced schema-level xs:element -->
  <xsl:template match="xs:element[@ref]">
    <xsl:variable name="ref-name" select="@ref" as="xs:string" />
    <!-- the mode specified below doesn't matter; it just
         mustn't be the default mode -->
    <xsl:apply-templates
        select="/xs:schema/xs:element[@name=$ref-name]"
        mode="inline" />
  </xsl:template>

  <!-- in the default mode, transform schema-level
       xs:element elements to nothing -->    
  <xsl:template match="/xs::schema/xs:element[@name]" />

</xsl:stylesheet>

Note that regardless of implementation, the requested operation can require infinite recursion.

Upvotes: 1

Related Questions