Search code examples
pythonxmlxslttransform

XSL for each text starts-with select in variable


i have this XML in input:

 <root> RH03051CDSIA280524CM1490301951171                                                                                      
 610000001                  93001                  G0305101700000000004575EUR270524C000000000000,00IT44                 
 620000001001270524270524D000000000649,3450TE                ITDA00DPN145                                               
 630000001001HAYS SRL/AVIS BUDGET ITALIA S.P.A./AR885265/2355070853/B2B/RCUR/OE5OA5200P4907R3                           
 640000001EUR270524C000000000000,00                                                                                     
 EF03051CDSIA280524CM1490301951171           0000001                              0000006                               
 RH03051CDSIA280524CM1490301951349                                                                                      
 610000001                  93001                  Z0305101699000078389249USD270524C000000001320,97IT72                 
 640000001USD270524C000000001320,97                                                                                     
 EF03051CDSIA280524CM1490301951349           0000001                              0000004                               
</root>

i want iterate for each string starts with " RH" for parse whole multiline string. for example in my xml input i have 2 strings that starts with RH:

  1. RH03051CDSIA280524CM1490301951171 ....
  2. RH03051CDSIA280524CM1490301951349 ....

my xslt is this:

    <xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                xmlns:ns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.03"
                xmlns:BODY="urn:CBI:xsd:CBIBdyBkToCstmrStmtReq.00.01.02"
                xmlns:LMSG="urn:CBI:xsd:CBIBkToCstmrStmtReqLogMsg.00.01.02"
                xmlns:HE2E="urn:CBI:xsd:CBIHdrSrv.001.07"
                xmlns:HTRT="urn:CBI:xsd:CBIHdrTrt.001.07"
                xmlns:IDST="urn:CBI:xsd:CBIIdyStmtReqLogMsg.00.01.02"
                xmlns:DLST="urn:CBI:xsd:CBIDlyStmtReqLogMsg.00.01.02"
                xmlns:PRST="urn:CBI:xsd:CBIPrdcStmtReqLogMsg.00.01.02"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:str="http://exslt.org/strings"
                xmlns:myns="py_ns"
                extension-element-prefixes="exsl myns"
                exclude-result-prefixes="exsl ns">


    <xsl:output encoding="UTF-8" standalone="yes" indent="yes" method="xml" />

    <xsl:template match="/">
                
        <xsl:for-each select="//text[starts-with(., ' RH')]">
            <xsl:value-of select="." />
            
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

but when i do:

<xsl:for-each select="//text[starts-with(., ' RH')]">
<xsl:value-of select="." />

i dont view nothing. is correct my code? i must use lxml library and i use : xmlns:exsl="http://exslt.org/common" and xmlns:str="http://exslt.org/strings"

i want:

RH03051CDSIA280524CM1490301951171 610000001 93001 G030510170 ......................... 0000001

and this:

RH03051CDSIA280524CM1490301951349 610000001 93001 ......................................... EF03051CDSIA280524CM1490301951349 0000004

same tips? thanks regards


Solution

  • Using the libsxlt processor (or any XSLT 1.0 processor that supports the EXSLT str:tokenize() extension function) you can do:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    extension-element-prefixes="str">
    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    
    <xsl:template match="/">
        <strings>
            <xsl:for-each select="str:tokenize(normalize-space(root), ' ')[starts-with(., 'RH')]">
                <string>
                    <xsl:value-of select="."/>
                </string>
            </xsl:for-each>
        </strings>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Added:

    The result you want is still not clear. I am guessing (!) you want to do something like:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    extension-element-prefixes="str">
    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    
    <xsl:template match="/">
        <strings>
            <xsl:for-each select="str:split(root, ' RH')">
                <string>
                    <xsl:value-of select="concat('RH', .)"/>
                </string>
            </xsl:for-each>
        </strings>
    </xsl:template>
    
    </xsl:stylesheet>