代码之家  ›  专栏  ›  技术社区  ›  Catanzaro

每个文本的XSL以select in变量开头

  •  0
  • Catanzaro  · 技术社区  · 1 年前

    我输入了以下XML:

     <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>
    

    我想对每个以“RH”开头的字符串进行迭代,以解析整个多行字符串。例如,在我的xml输入中,我有两个以RH开头的字符串:

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

    我的xslt是这样的:

        <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>
    

    但当我这样做时:

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

    我什么都不看。 我的代码正确吗? 我必须使用lxml库,并且我使用: xmlns:exsl=“http://exslt.org/common" 和 xmlns:str=“http://exslt.org/strings"

    我想要:

    RH03051CDSIA280524CM1490301951171 610000001 93001 G030510170。。。。。。。。。。。。。。。。。。。。。。。。。0000001

    并且这个:

    RH03051CDSIA280524CM1490301951349 610000001 93001。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。EF03051CDSIA280524CM1490301951349 0000004

    相同的提示? 谢谢 当做

    2 回复  |  直到 1 年前
        1
  •  1
  •   Martin Honnen    1 年前

    文本位于作为根元素的子元素的单个文本节点中;在XSLT3中,您可以使用例如。

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="#all"
      expand-text="yes">
    
      <xsl:output method="text"/>
    
      <xsl:template match="/" name="xsl:initial-template">
        <xsl:value-of select="tokenize(root, '\n')[starts-with(., ' RH')]" separator="&#10;"/>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Output is 例如

     RH03051CDSIA280524CM1490301951171                                                                                      
     RH03051CDSIA280524CM1490301951349  
    

    XSLT3可以与Python一起使用,使用 saxonche package .`

        2
  •  0
  •   michael.hor257k    1 年前

    使用 libsxlt 处理器(或任何支持EXSLT的XSLT1.0处理器 str:tokenize() 扩展功能),您可以做到:

    <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>
    

    补充:

    你想要的结果仍然不清楚。我猜(!)你想做这样的事情:

    <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>