开发者

XSLT modify value

开发者 https://www.devze.com 2023-02-27 08:35 出处:网络
I have XML that I need to transform into another XML. What I need to do is modify the value of one of the element like this. In the source I have this

I have XML that I need to transform into another XML.

What I need to do is modify the value of one of the element like this. In the source I have this

<NUM>I19071230</NUM>

I need to replace the first letter with 'ABCD' which is fixed. So it will become t开发者_JAVA百科his

ABCD19071230

Then I need to make sure the total length is 14. If it is not then add 0s between ABCD and the output should look like this.

ABCD0019071230

I need to store this value in a variable so it can be reused. I'm using XSLT 1.0. Thanks in Advance.


Use:

<xsl:variable name="vComposed" select=
   "concat('ABCD',
           substring('000000000000000',
                     1,
                     14 -string-length(NUM) -3),
           substring(NUM, 2, 10)
           )
 "/>

Here is a short transformation using this variable:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:variable name="vComposed" select=
   "concat('ABCD',
           substring('000000000000000',
                     1,
                     14 -string-length(NUM) -3),
           substring(NUM, 2, 10)
           )
 "/>

 <xsl:value-of select="$vComposed"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<NUM>I19071230</NUM>

the wanted, correct result is produced:

ABCD0019071230

You can alter the length of the string value of NUM and verify that the correct result is produced in all cases.


Use this XPath expression:

concat(
   substring(
      'ABCD0000000000',
      1,
      15 - string-length(NUM)
   ),
   substring(NUM,2)
)
0

精彩评论

暂无评论...
验证码 换一张
取 消