Re: [xsl] Algorithm for setting a variable number of attributes

Subject: Re: [xsl] Algorithm for setting a variable number of attributes
From: "John Lumley john@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 2 Aug 2022 21:05:36 -0000
Both my and Normbs implementations contain XSLT APIs to allow running in
SaxonJS (browser) and SaxonJ programs respectively. Links from
invisiblexml.org

John Lumley

Sent from my iPad

> On 2 Aug 2022, at 21:11, Steven D. Majewski steve.majewski@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> o;?
>
> Not a direct answer to this question, but I was just thinking, while going
thru the Invisible XML https://invisiblexml.org intro and tutorial that it
would sure be handy if the parser was a callable function from XSLT or XQuery
b writing a grammar for things like human written variations of dates would
be nicer to deal with than regex code. There are XQuery and XSLT
implementations, so maybe itbs doable.
>
> b Steve M.
>
>
>> On Aug 2, 2022, at 2:50 PM, Martin Honnen martin.honnen@xxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>>
>>
>>> On 02.08.2022 19:47, rick@xxxxxxxxxxxxxx wrote:
>>> Hello All,
>>>
>>> I have this as my input:
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <root>
>>>     <subtask>SUBTASK 25-31-04-714-080 - (20130315)</subtask>
>>> </root>
>>>
>>> And this is my desired output:
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <root>
>>>     <subtask chapnbr="25" sectnbr="31" subnbr="04" func="714" seq="080"
revdate="20130315"/>
>>> </root>
>>>
>>> I am using a function to parse the input and get it down to a tokenized
sequence. Sometimes the input may not have all of the same positions of data;
for example, it may be:
>>>
>>> <subtask>SUBTASK 25-31-04</subtask>
>>>
>>> I can use a series of <xsl:if> statements to add the appropriate attribute
values based on count($attributes), but that makes the <xsl:template
match=bsubtaskb> a bit busy. I am wondering if there is a better way to do
this. For example, can I add the attributes to the element inside my function?
Here is my XSLT. Thank you.
>>>
>>
>> You can use xsl:if or xsl:choose/xsl:when of course inside of the function
itself, as well as XPath
>>
>>   if (expression) then expression else expression
>>
>> instead.
>>
>> It is not clear which values can be missing and how you want to complete
them.
>>
>> As for other approaches, there is xsl:analyze-string and in XSLT 3/XPath
3.1 there is also the XPath analyze-string function you could try with a
pattern for the various groups of digits you expect, push the result through a
mode that adds the default data for missing groups and push through another
mode perhaps that outputs the attribute nodes.
>>
>>
>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>>>     xmlns:xs="http://www.w3.org/2001/XMLSchema";
>>>     xmlns:math="http://www.w3.org/2005/xpath-functions/math";
>>>     xmlns:rq="http://www.frameexpert.com/functions";
>>>     exclude-result-prefixes="xs math rq"
>>>     version="3.0" expand-text="yes">
>>>
>>>     <xsl:output indent="yes"/>
>>>
>>>     <xsl:template match="/">
>>>         <root>
>>>             <xsl:apply-templates/>
>>>         </root>
>>>     </xsl:template>
>>>
>>>     <xsl:template match="subtask">
>>>         <xsl:copy>
>>>             <xsl:variable name="attributes"
select="rq:getSubtaskAttributes(.)"/>
>>>             <xsl:message>{$attributes}</xsl:message>
>>>         </xsl:copy>
>>>     </xsl:template>
>>>
>>>     <xsl:function name="rq:getSubtaskAttributes">
>>>         <xsl:param name="context"/>
>>>         <xsl:variable name="variable1"
select="replace($context,'[^-\d]+','')"/>
>>>         <xsl:sequence select="tokenize($variable1,'-')"/>
>>>     </xsl:function>
>>>
>>> </xsl:stylesheet>
>>> XSL-List info and archive
>>> EasyUnsubscribe (by email)
>> XSL-List info and archive
>> EasyUnsubscribe (by email)
>
> XSL-List info and archive
> EasyUnsubscribe (by email)

Current Thread