Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This transformation</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" &gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:param name="ptopElementName" select="'RootElement'"/&gt; &lt;xsl:variable name="vTop" select= "/*/xs:element[@name=$ptopElementName]"/&gt; &lt;xsl:variable name="vNames" select="$vTop/descendant-or-self::*/@name"/&gt; &lt;xsl:variable name="vRefs" select="$vTop/descendant-or-self::*/@ref"/&gt; &lt;xsl:variable name="vTypes" select="$vTop/descendant-or-self::*/@type"/&gt; &lt;xsl:template match="node()|@*" name="identity"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="xs:element"&gt; &lt;xsl:if test= "@name=$vNames or @name=$vRefs or ancestor-or-self::*[@name=$ptopElementName]"&gt; &lt;xsl:call-template name="identity"/&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;xsl:template match="xs:complexType|xs:simpleType"&gt; &lt;xsl:if test= "@name=$vTypes or ancestor-or-self::*[@name=$ptopElementName]"&gt; &lt;xsl:call-template name="identity"/&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the provided XML document:</strong></p> <pre><code>&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt; &lt;xs:element name="RootElement"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="ChildElement"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt;&lt;/xs:element&gt; &lt;xs:element name="ChildElement"/&gt; &lt;xs:element name="UnusedElement"/&gt; &lt;/xs:schema&gt; </code></pre> <p><strong>produces the wanted, corect result:</strong></p> <pre><code>&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt; &lt;xs:element name="RootElement"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="ChildElement"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="ChildElement"/&gt; &lt;/xs:schema&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COA brave effort but it looks to me as if it won't work if the schema document has a target namespace, or if it includes/imports other schema documents. Processing XSD documents in their full generality using XSLT is hard - it's quite possible if you know that you're only using a subset of the XSD language, but very challenging if the use of XSD is unconstrained. (Also, note that an element declaration can appear to be "unused" but still affect what's allowed in a strict wildcard (`<xs:any processContents="strict"/>`)
      singulars
    2. CO@Michael-Kay: Yes, I fully realize that this solution is very limited to the OP's stated requirements. I would need a good refresh of my rusty XSD knowledge in order to claim that the solution covers even all major cases. Also, it seems to me that referencing names and types is transitive and as such needs to be done in many steps in order to construct the transitive closure of the "references/referenced" relation -- this, of course, is not done here. Would be a lot of work even for a bounty.
      singulars
    3. COThanks Dimitre, that's certainly a pretty cool solution (and quick response). Unfortunately Michael was right about the target namespace thing - my sample was a very simplified version whereas my actual schemas have target namespaces in them. Interestingly it also removed the XML declaration and all the type definitions (not sure if the types removal is due to the same thing).
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload