[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"
  <xs:complexType name="IssuerSummaryType">
      <xs:element name="recordCode">
          <xs:restriction base="Text2Type">
            <xs:enumeration value="01"/>
      <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"/>

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

      <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>

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]}>

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.

exclude-result-prefixes="xs" version="3.0">
  <xsl:import href="identity.xsl"/>
  <xsl:template match="xs:sequence/xs:element">
      <xsl:attribute  name="count" select="position()"/>
      <xsl:apply-templates select="@*|node()"/>

Weirdly  I get the counter going up in increments of 2

 <xs:complexType name="IssuerSummaryType">
         <xs:element count="2" name="recordCode">
               <xs:restriction base="Text2Type">
                  <xs:enumeration value="01"/>
         <xs:element count="4" name="tradingPartner"
         <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"/>

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