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

Subject: Re: [xsl] Algorithm for setting a variable number of attributes
From: "Joel Kalvesmaki director@xxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 3 Aug 2022 00:22:50 -0000
Let me suggest a possibly easier way to solve this little problem:

<xsl:template match="subtask">
<xsl:variable name="attribute-holder" as="element()">
<holder>
<xsl:analyze-string select="." regex="^SUBTASK (\d+)-(\d+)-(\d+)-(\d+)-(\d+) - \((\d+)\)$">
<xsl:matching-substring>
<xsl:attribute name="chapnbr" select="regex-group(1)"/>
<xsl:attribute name="sectnbr" select="regex-group(2)"/>
<xsl:attribute name="subnbr" select="regex-group(3)"/>
<xsl:attribute name="func" select="regex-group(4)"/>
<xsl:attribute name="seq" select="regex-group(5)"/>
<xsl:attribute name="revdate" select="regex-group(6)"/>
</xsl:matching-substring>
</xsl:analyze-string>
<xsl:analyze-string select="." regex="^SUBTASK (\d+)-(\d+)-(\d+)$">
<xsl:matching-substring>
<xsl:attribute name="chapnbr" select="regex-group(1)"/>
<xsl:attribute name="sectnbr" select="regex-group(2)"/>
<xsl:attribute name="subnbr" select="regex-group(3)"/>
</xsl:matching-substring>
</xsl:analyze-string>
</holder>
</xsl:variable>


<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:choose>
<xsl:when test="exists($attribute-holder/@*)">
<xsl:message select="$attribute-holder/@*"/>
</xsl:when>
<xsl:otherwise>
<xsl:message select="'Uh oh, I do not recognize the pattern ' || string(.)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>


You can fine-tune the regular expressions to be as restrictive or loose as needed.

jk


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"; [1]


xmlns:xs="http://www.w3.org/2001/XMLSchema"; [2]


xmlns:math="http://www.w3.org/2005/xpath-functions/math"; [3]

xmlns:rq="http://www.frameexpert.com/functions"; [4]

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 [5]
EasyUnsubscribe [6] (by email)

XSL-List info and archive [5] EasyUnsubscribe [7] (by email)

Links:
------
[1] http://www.w3.org/1999/XSL/Transform
[2] http://www.w3.org/2001/XMLSchema
[3] http://www.w3.org/2005/xpath-functions/math
[4] http://www.frameexpert.com/functions
[5] http://www.mulberrytech.com/xsl/xsl-list
[6] http://lists.mulberrytech.com/unsub/xsl-list/582271
[7] http://lists.mulberrytech.com/unsub/xsl-list/3422410

-- Joel Kalvesmaki Director, Text Alignment Network http://textalign.net

Current Thread