xsl-list
[Top] [All Lists]

Re: [xsl] Need help with XSLT tokenize

2020-08-11 13:25:20
This XSLT 3.0 transformation:

<xsl:stylesheet version="3.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="invoiceDetails">
    <Orders>
      <xsl:apply-templates select="tokenize(., ';')" mode="m-order"/>
    </Orders>
  </xsl:template>

  <xsl:template match=".[. instance of xs:string]" mode="m-order">
    <Order>
      <xsl:apply-templates select="tokenize(., '#')[1]" mode="m-detail-1"/>
      <xsl:apply-templates select="tokenize(., '#')[2]" mode="m-detail-2"/>
    </Order>
  </xsl:template>

  <xsl:template match=".[. instance of xs:string]" mode="m-detail-1">
    <OrderNumber><xsl:apply-templates select="."/></OrderNumber>
  </xsl:template>

  <xsl:template match=".[. instance of xs:string]" mode="m-detail-2">
    <Amount><xsl:apply-templates select="."/></Amount>
  </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Payments>
  <Payment>
    <invoiceDetails>order1#amt1;order2#amt2;</invoiceDetails>
  </Payment>
</Payments>

produces the wanted, correct result:

<Orders>
   <Order>
      <OrderNumber>order1</OrderNumber>
      <Amount>amt1</Amount>
   </Order>
   <Order>
      <OrderNumber>order2</OrderNumber>
      <Amount>amt2</Amount>
   </Order>
   <Order/>
</Orders>



On Tue, Aug 11, 2020 at 4:34 AM Prady Prady 
prady(_dot_)chin(_at_)gmail(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Team,

I need help with XSLT. Below is my requirement. Can somebody help?
=====================
Input xml:

<Payments>

<Payment>

<invoiceDetails>order1#amt1;order2#amt2;</invoiceDetails>

</Payment>

</Payments>

I need to be able to convert this to:
Output xml:

<Orders>

<Order>

<OrderNumber>order1</OrderNumber>

<Amount>amt1</Amount>

</Order>

<Order>

<OrderNumber>order2</OrderNumber>

<Amount>amt2</Amount>

</Order>

</Orders>
======================

Thank you very much for your help

XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/782854> (by
email <>)



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>