Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT: Copy Where current type value equals the name of an element
    text
    copied!<p>Using <a href="http://www.pesc.org/library/docs/standards/Sector%20Library/AcademicRecord%5Fv1.4.0.xsd" rel="nofollow noreferrer">this file</a> as source, I have a situation where I need to retrieve an element from either the local source file or a related one noted in the imports. The type value uses a colon to separate the two values - substring-before(@type, ':') tells me which file to reference; substring-after(@type, ':') is the name of the element in the file I need to copy &amp; iterate over its contents in the same fashion.</p> <p>Example: I want the xs:complexType where the name is "PersonType", so I use the copy-of to grab it and its children. The next step is to look at those children - for those that are xs:element, I want to retrieve the element referenced in the type value ("AcRec:HighSchoolType"). The "AcRec" tells me which xsd I need to use, so I know I'll find something in that xsd where the name value is "HighSchoolType". Looking at the AcRec xsd, I know that "HighSchoolType" is an xs:complexType (which I already have a template defined to handle) so I should see the output.</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &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:template match="/"&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:core="urn:org:pesc:core:CoreMain:v1.2.0" xmlns:AcRec="urn:org:pesc:sector:AcademicRecord:v1.1.0"&gt; &lt;xsl:apply-templates select="//xs:complexType[@name='PersonType']" /&gt; &lt;/xs:schema&gt; &lt;/xsl:template&gt; &lt;xsl:template match="xs:complexType"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="node()[not(xs:annotation | xs:restriction)]|@*"/&gt; &lt;/xsl:copy&gt; &lt;xsl:apply-templates select=".//xs:element" /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="xs:simpleType"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="xs:element"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="substring-before(@type, ':') = 'AcRec'"&gt; &lt;xsl:text&gt;AcRec&lt;/xsl:text&gt; &lt;xsl:apply-templates select="document('[local file path]AcademicRecord_v1.3.0.xsd')//*[@name=substring-after(@type, ':')]" /&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; </code></pre> <p> </p> <p>Desired output would look like:</p> <pre><code>&lt;xs:complexType name="PersonType"&gt; &lt;xs:sequence&gt; &lt;xs:element name="HighSchool" type="AcRec:HighSchoolType" minOccurs="0"&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="HighSchoolType"&gt; &lt;xs:sequence&gt; &lt;xs:element name="OrganizationName" type="core:OrganizationNameType"/&gt; &lt;xs:group ref="core:OrganizationIDGroup" minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; </code></pre> <p>What am I missing about looking within the document when I successfully enter the xsl:when? The xsl:text tells me I'm in, but the subsequent line returns no output.</p> <p>Additionally, how do I exclude xs:annotation and xs:restriction elements from appearing when copying the xs:complextType &amp; xs:simpleType elements? I haven't been able to get the examples mentioned on the dpawson site to work.</p>
 

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