Subject: Re: [xsl] How to store a sequence into an element ... and maintainthe sequence inside the element? From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Sun, 19 Jul 2020 21:17:47 -0000 |
Hi Roger, > Is there no other way to preserve a sequence than to resort to connecting the XSLT to an XML Schema? There are many possible ways. Here are two types of these: <list> <item>4</item> <item>1</item> <item>4</item> </list> or <list items="4|1|4"/> To get the count: count(/list/item) or count(tokenize(/list/@items, '|')) In case you are using XSLT 3, you could represent the list of items in a string that is an XPath expression (such as a sequence, array, ... ), then retrieve that list from the XML document using the <xsl:evaluate> instruction of XSLT 3 and you have it. Not sure about the "most efficient" form, this depends on the definition of efficiency (time/space/readability,... etc) and on the nature of the specific problem being solved. In case you are using BaseX, they have implemented sequences with the Finger Tree functional data structure, which has time efficiency of O(log(N)) for **all** operations on sequences -- insert/delete/update/replace/index and this is done behind the scenes so you just use the regular sequence-manipulation functions/operators as per the XPath/XQuery Functions and Operators standard. One can even use my 12-years old implementation of the Finger Tree data structure, using extension functions, as I did then and compared the efficiency in this blog article: https://dnovatchev.wordpress.com/2008/09/08/part-3-the-swiss-army-knife-of-data-structures-in-c/ On Sun, Jul 19, 2020 at 1:52 PM Dr. Roger L Costello costello@xxxxxxxxx < xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: > Thank you Martin. Is there no other way to preserve a sequence than to > resort to connecting the XSLT to an XML Schema? > > In my actual application I am creating many sequences. I need an efficient > way of storing the sequences. For example, initially $list might be (1, 2, > 3) and then processing results in updating $list to (4, 1, 5) and then to > (9, 9, 0) and then to (4, 1, 4) and then to ... > > What is your recommendation for managing sequences? > > /Roger > > -----Original Message----- > From: Martin Honnen martin.honnen@xxxxxx < > xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> > Sent: Sunday, July 19, 2020 3:57 PM > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: [EXT] Re: [xsl] How to store a sequence into an element ... and > maintainthe sequence inside the element? > > On 19.07.2020 21:14, Dr. Roger L Costello costello@xxxxxxxxx wrote: > > Hi Folks, > > > > I created a sequence (1, 2, 3) as follows: > > > > <xsl:variable name="list" select="(1, 2, 3)" as="xs:integer*" /> > > > > I checked that $list contains 3 integers: > > > > <xsl:message>count($list) = <xsl:value-of > select="count($list)"/></xsl:message> > > > > Sure enough, the output shows that there are 3 items: > > > > count($list) = 3 > > > > Next, I want to store that sequence of integers in an element, along > with other elements: > > > > <xsl:variable name="document" as="element(Document)"> > > <Document> > > <Greeting>My List</Greeting> > > <List><xsl:sequence select="$list" /></List> > > <Ending>The End</Ending> > > </Document> > > </xsl:variable> > > > > Does the <List> element contain a sequence of 3 integers: > > > > <xsl:message>count($document/List) = <xsl:value-of > select="count($document/List)"/></xsl:message> > > > > Yikes! No it doesn't: > > > > count($document/List) = 1 > > > > How to do what I want to do? That is, how to store the sequence of > integers in an element, along with other elements, such that I can pull the > sequence out of the element and immediately operate on the sequence? If > that can't be done, then what's the right way to do what I want to do? > > That expression will always return the number of "List" elements. If you > use a schema and give that element a list datatype then I think that > with schema-aware processing > $document/List/data() > would give you a list and you could of course count that list with e.g. > count($document/List/data()) > > > So the simplest example would be > > <xsl:import-schema> > <xs:schema> > <xs:element name="List" type="int-list"/> > <xs:simpleType name="int-list"> > <xs:list itemType="xs:integer"/> > </xs:simpleType> > </xs:schema> > </xsl:import-schema> > > <xsl:template match="/" name="xsl:initial-template"> > <xsl:variable name="list" select="(1, 2, 3)" as="xs:integer*" /> > <xsl:message>count($list) = <xsl:value-of > select="count($list)"/></xsl:message> > <xsl:variable name="list-element" as="schema-element(List)"> > <List xsl:validation="strict"> > <xsl:sequence select="$list"/> > </List> > </xsl:variable> > <xsl:message>count($list-element) = <xsl:value-of > select="count($list-element)"/></xsl:message> > <xsl:message>count($list-element/data()) = <xsl:value-of > select="count($list-element/data())"/></xsl:message> > <xsl:message select="$list-element instance of > schema-element(List)"/> > </xsl:template> > > you would need to write a more complex schema for the more complex > sample you had I think, I am not sure whether you could use a schema > type for that element inside other untyped elements. > > > -- 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] How to store a sequence i, Martin Honnen martin | Thread | Re: [xsl] How to store a sequence i, Dr. Roger L Costello |
Re: [xsl] How to store a sequence i, Martin Honnen martin | Date | Re: [xsl] How to store a sequence i, Dr. Roger L Costello |
Month |