在XSLT 1.0中将整数值转换为字符串

huangapple go评论60阅读模式
英文:

Converting integer values to string in XSLT 1.0

问题

You can convert the integer value to a string within XSLT by using the string() function. Here's how you can modify your XSLT to achieve the expected JSON output:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="no" method="xml"/>
    <xsl:template match="/">
        <Session>
            <xsl:variable name="profileID" select="string(profileid)"/>
            <profileId>
                <xsl:value-of select="$profileID"/>
            </profileId>
        </Session>
    </xsl:template>
</xsl:stylesheet>

With this XSLT, the value of <profileID> will be converted to a string, resulting in the expected JSON output:

{
	"Session": {
		"profileId": "452628"
	}
}
英文:

I have an element in my XSLT named profileid. The value to this element can be expected as a set of numbers that is right now going as integer(eg. <profileID>452628</profileID>), but this value has to be passed as a string to the next process. Is there a way I can convert this integer value to string within xslt itself? [I am using XSLT 1.0]

XSLT I've used:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
        &lt;xsl:output indent=&quot;no&quot; method=&quot;xml&quot;/&gt;
        &lt;xsl:template match=&quot;/&quot;&gt;
              &lt;Session&gt;
                &lt;xsl:variable name=&quot;profileID&quot; select=&quot;concat(&#39; &#39;,$profileid)&quot;/&gt;
                &lt;profileId&gt;
                    &lt;xsl:value-of select=&quot;translate($profileID,&#39; &#39;,&#39;&#39;)&quot;/&gt;
                &lt;/profileId&gt;
            &lt;/Session&gt;
        &lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;

(Converted the xml response to json)

JSON output :-

{
&quot;Session&quot;: {
&quot;profileId&quot;: 452628
}
}

Expected JSON output:_

{
&quot;Session&quot;: {
&quot;profileId&quot;: &quot;452628&quot;
}
}

A way to convert this integer value to String inside xslt

答案1

得分: 2

在XSLT转换后,假设您希望将所有属性作为字符串值。使用JSON Transform Mediator设置以下属性:

<jsontransform>
    <property name="synapse.commons.json.output.autoPrimitive" value="false"/>
</jsontransform>
英文:

Assuming you want all the attributes as String values. After the XSLT transformation set the following property using JSON Transform Mediator

&lt;jsontransform&gt;
    &lt;property name=&quot;synapse.commons.json.output.autoPrimitive&quot; value = &quot;false&quot;/&gt;
&lt;/jsontransform&gt;

huangapple
  • 本文由 发表于 2023年2月7日 03:57:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定