Wanted: xsl: a template that converts a node to its six-digit value?

I can almost say here: "What does the @ ## $ # hexavigesimal value hexavigesimal ?"

In a six-character number system, there is a base of twenty-six. Alternatively, base-26 can be represented using only the letters of the Latin alphabet. Since there are 26 letters in English, base-26 is also the highest base in which it is possible, and therefore uses each letter. 0 represents A, 1 = B, 2 = C ... 24 = Y, 25 = Z. Some examples: 26 = AA, 30 = BE

So basically, Excel uses a column description. I would like to have a function that converts a node value with an int value to this value.

A source:

 <root> <row>12</row> <column>23</column> </root> 

I would like to print column as X , invoking a template that performs the conversion. It can be done?

+4
source share
3 answers

Try this XSLT ...

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:variable name="symbols" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" /> <xsl:variable name="symbols-count" select="string-length($symbols)" /> <xsl:template match="row"> <row> <xsl:call-template name="convert" /> </row> </xsl:template> <xsl:template name="convert"> <xsl:param name="value" select="number(.)" /> <xsl:choose> <xsl:when test="$value >= $symbols-count"> <xsl:variable name="div" select="floor($value div $symbols-count)" /> <xsl:variable name="remainder" select="$value - $div * $symbols-count" /> <xsl:call-template name="convert"> <xsl:with-param name="value" select="$div" /> </xsl:call-template> <xsl:value-of select="substring($symbols, $remainder + 1, 1)" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="substring($symbols, $value + 1, 1)" /> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 

For the following XML

 <root> <row>12</row> <column>23</column> <row>26</row> <column>23</column> </root> 

The following is displayed:

 <root> <row>M</row> <column>23</column> <row>BA</row> <column>23</column> </root> 

You should be able to configure a character variable to allow any conversion with name-naming-imal. For example, to convert to hexadecimal change, follow these steps:

 <xsl:variable name="symbols" select="'0123456789ABCDEF'" /> 

And for binary

 <xsl:variable name="symbols" select="'01'" /> 
+1
source

You may find that <xsl:number value="$n" format="A"/> does what you want, but this is not guaranteed.

0
source

In XSLT 1.0 use :

  <xsl:template name="toHex"> <xsl:param name="decimalNumber" /> <xsl:if test="$decimalNumber >= 16"> <xsl:call-template name="toHex"> <xsl:with-param name="decimalNumber" select="floor($decimalNumber div 16)" /> </xsl:call-template> </xsl:if> <xsl:value-of select= "substring($hexDigits, ($decimalNumber mod 16) + 1, 1)" /> </xsl:template> 

where $hexDigits is the string '0123456789ABCDEF' .

Here is a complete example :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="hexDigits" select="'0123456789ABCDEF'"/> <xsl:template match="/*"> <xsl:call-template name="toHex"/> </xsl:template> <xsl:template name="toHex"> <xsl:param name="decimalNumber" select="." /> <xsl:if test="$decimalNumber >= 16"> <xsl:call-template name="toHex"> <xsl:with-param name="decimalNumber" select= "floor($decimalNumber div 16)" /> </xsl:call-template> </xsl:if> <xsl:value-of select= "substring($hexDigits, ($decimalNumber mod 16) + 1, 1)" /> </xsl:template> </xsl:stylesheet> 

When this XSLT 1.0 transformation is applied to this XML document:

 <t>12345</t> 

the desired, correct result is output:

 3039 

See also my answer to this question :

fooobar.com/questions/1415042 / ...

0
source

Source: https://habr.com/ru/post/1414336/


All Articles