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

Subject: Re: [xsl] Algorithm for setting a variable number of attributes
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 2 Aug 2022 18:50:31 -0000
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>
>
> B B B  <subtask>SUBTASK 25-31-04-714-080 - (20130315)</subtask>
>
> </root>
>
> And this is my desired output:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <root>
>
> B B B  <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

 B  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";
>
> B B B  exclude-result-prefixes="xs math rq"
>
> B B B  version="3.0" expand-text="yes">
>
> B B B B <xsl:output indent="yes"/>
>
> B B B B <xsl:template match="/">
>
> B B B B B B B  <root>
>
> B B B B B B B B B B B  <xsl:apply-templates/>
>
> B B B B B B B  </root>
>
> B B B  </xsl:template>
>
> B B B B <xsl:template match="subtask">
>
> B B B B B B B  <xsl:copy>
>
> B B B B B B B B B B B  <xsl:variable name="attributes"
> select="rq:getSubtaskAttributes(.)"/>
>
> <xsl:message>{$attributes}</xsl:message>
>
> B B B B B B B  </xsl:copy>
>
> B B B  </xsl:template>
>
> B B B B <xsl:function name="rq:getSubtaskAttributes">
>
> B B B B B B B  <xsl:param name="context"/>
>
> B B B B B B B  <xsl:variable name="variable1"
> select="replace($context,'[^-\d]+','')"/>
>
> B B B B B B B  <xsl:sequence select="tokenize($variable1,'-')"/>
>
> B B B  </xsl:function>
>
> </xsl:stylesheet>
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/582271>
> (by email <>)

Current Thread