xsl-list
[Top] [All Lists]

Re: [xsl] Really want a zipper

2016-05-29 22:08:52
You forgot to present the wanted result.

On Sun, May 29, 2016 at 7:52 PM, Ihe Onwuka 
ihe(_dot_)onwuka(_at_)gmail(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Assume XSLT2.0 (but am willing to hear about 3.0 solutions)

Given this (cut down) schema.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
           xmlns="urn:midas"
           targetNamespace="urn:midas"
elementFormDefault="qualified">
  <xs:complexType name="IssuerSummaryType">
    <xs:sequence>
      <xs:element name="recordCode">
        <xs:simpleType>
          <xs:restriction base="Text2Type">
            <xs:enumeration value="01"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="tradingPartner" type="TradingPartnerType"/>
      <xs:element name="issuerStateCode" type="Text2Type"/>
      <xs:element name="HIOSid" type="HIOSIDType"/>
      <xs:element name="issuerExtractDate" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

I would like to substitute in the following instance with the element
names

<midas>
   <issuerSummary>
      <field id="1">01</field>
      <field id="2">54631</field>
      <field id="3">VA</field>
      <field id="4">11512</field>
      <field id="5">04222016</field>
   </issuerSummary>

such that the field with @id =1 has element name recordCode and the field
with @id=2 has element name tradingPartner etc.

So you see why I say I really need a zipper as  I want to interleave the
element names from the schema with the data in the instance.

The @id attributes in the instance are actually a hack, they were supposed
to support something along the lines of

<xsl:template match="field">
   <xsl:element name="{xs:element[current()/@id]}>
     <xsl:apply-templates/>
   </xsl:element>
 </xsl:template>

However I keep getting a zero-length string error on the xsl:element.

Even more hackish I thought of creating a mock schema to use for the
transformation and sticking a count attribute on the element like so.

<xsl:stylesheet
        xmlns:xs="http://www.w3.org/2001/XMLSchema";
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
exclude-result-prefixes="xs" version="3.0">
  <xsl:import href="identity.xsl"/>
  <xsl:template match="xs:sequence/xs:element">
    <xsl:copy>
      <xsl:attribute  name="count" select="position()"/>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
 </xsl:stylesheet>

Weirdly  I get the counter going up in increments of 2

 <xs:complexType name="IssuerSummaryType">
      <xs:sequence>
         <xs:element count="2" name="recordCode">
            <xs:simpleType>
               <xs:restriction base="Text2Type">
                  <xs:enumeration value="01"/>
               </xs:restriction>
            </xs:simpleType>
         </xs:element>
         <xs:element count="4" name="tradingPartner"
type="TradingPartnerType"/>
         <xs:element count="6" name="issuerStateCode" type="Text2Type"/>
         <xs:element count="8" name="HIOSid" type="HIOSIDType"/>
         <xs:element count="10" name="issuerExtractDate" type="xs:date"/>
       </xs:sequence>
  </xs:complexType>

I'm all ears for a diagnosis and better ways of accomplishing this. There
should be an elegant solution to this and I expect my  hacks are taking me
further and further away from it.
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <-list/782854> (by
email <>)




-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>