[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] Really want a zipper
Subject: Re: [xsl] Really want a zipper From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Mon, 30 May 2016 03:24:38 -0000 |
If my guess about the wanted result is correct, then this transformation: <xsl:stylesheet version="2.0" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:variable name="vElemNames" select= "doc('midas.xsd')/*/*/*/xs:element/@name/string()"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="field"> <xsl:variable name="vPos" select="position()"/> <xsl:element name="{$vElemNames[$vPos]}"> <xsl:apply-templates select="node()|@*"/> </xsl:element> </xsl:template> </xsl:stylesheet> when applied on the provided XML document: <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> </midas> and referencing the provided XSD file (midas.xsd): <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> produces (what I guess is) the wanted, correct result: <midas> <issuerSummary> <recordCode id="1">01</recordCode> <tradingPartner id="2">54631</tradingPartner> <issuerStateCode id="3">VA</issuerStateCode> <HIOSid id="4">11512</HIOSid> <issuerExtractDate id="5">04222016</issuerExtractDate> </issuerSummary> </midas> Cheers, Dimitre Novatchev On Sun, May 29, 2016 at 7:52 PM, Ihe Onwuka ihe.onwuka@xxxxxxxxx < xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> 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.
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Really want a zipper, Dimitre Novatchev dn | Thread | Re: [xsl] Really want a zipper, Ihe Onwuka ihe.onwuk |
Re: [xsl] Really want a zipper, Dimitre Novatchev dn | Date | Re: [xsl] Really want a zipper, Ihe Onwuka ihe.onwuk |
Month |