[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Really want a zipper


Subject: [xsl] Really want a zipper
From: "Ihe Onwuka ihe.onwuka@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 30 May 2016 02:52:45 -0000

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.


Current Thread
Keywords