2017-07-18 51 views
0

我需要获取每个'/'的值,但由于文件中包含特殊字符,我无法获得正确的输出。我如何使用正确的正则表达式来检查?我正在使用<xsl:analyze-string>元素来获取值。这里是我的示例文件:如何使用正确的正则表达式

INPUTFILE:

<Communication> 
    <DialNumber>Phone/+31-3424-27385/null/Phone/+06-32-7890-565/Mobile(Office)/null/+313-(424)-28500/Fax</DialNumber> 
</Communication> 

预期输出

<Communication> 
    <ChannelCode>Phone</ChannelCode> 
    <UseCode>null</UseCode> 
    <DialNumber>+31-3424-27385</DialNumber> 
</Communication> 
<Communication> 
    <ChannelCode>Phone</ChannelCode> 
    <UseCode>Mobile(Office)</UseCode> 
    <DialNumber>+06-32-7890-565</DialNumber> 
</Communication> 
<Communication> 
    <ChannelCode>null</ChannelCode> 
    <UseCode>Fax</UseCode> 
    <DialNumber>+313-(424)-28500</DialNumber> 
</Communication> 

XSLTCode

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="DialNumber"> 
    <xsl:analyze-string select="normalize-space()" regex="(\w+)/(\w+)/(\w+)"> 
     <xsl:matching-substring> 
      <Communication> 
        <ChannelCode> 
         <xsl:value-of select="regex-group(1)"/> 
        </ChannelCode> 
        <UseCode> 
         <xsl:value-of select="regex-group(3)"/> 
        </UseCode> 
       <DialNumber> 
        <xsl:value-of select="regex-group(2)"/> 
       </DialNumber> 
      </Communication> 
     </xsl:matching-substring> 
    </xsl:analyze-string> 
</xsl:template> 
</xsl:stylesheet> 

我需要检查'/'后面的第3个单词以及后面的3个单词,最后3个单词。它看起来像这样:

Phone/+31-3424-27385/null

Phone/+06-32-7890-565/Mobile(Office)

null/+313-(424)-28500/Fax

首届正则表达式,我需要在<Channel>分配,在<DialNumber>第二和第三的<UseCode>

非常感谢您的反馈。

回答

1

如果 - 因为它似乎 - 你输入三个一组组织,你可以做简单:

<xsl:template match="Communication"> 
    <xsl:for-each-group select="tokenize(DialNumber, '/')" group-by="(position()-1) idiv 3"> 
     <Communication> 
      <ChannelCode> 
       <xsl:value-of select="current-group()[1]" /> 
      </ChannelCode> 
      <UseCode> 
       <xsl:value-of select="current-group()[3]" /> 
      </UseCode> 
      <DialNumber> 
       <xsl:value-of select="current-group()[2]" /> 
      </DialNumber> 
     </Communication>   
    </xsl:for-each-group> 
</xsl:template> 

演示:http://xsltransform.net/3MvmrA5/1

+0

感谢您的反馈。 – user7918368

0

您可以取代你的analyze-string如果分隔符是/字符如下:

<xsl:analyze-string select="normalize-space()" regex="(.+?)/(.+?)/(.+?)(/|$)" > 

这里,(.+?)/做一个懒惰寻找一个/以前匹配的字符集。并且/|$将考虑斜线后的最后一个令牌,因为$表示字符串的结尾。

+0

感谢您的反馈。 – user7918368

1

无需analyze-string这里,tokenize()将工作细

<xsl:variable name="tokens" select="tokenize(., '/')"/> 
<xsl:for-each-group select="$tokens" group-adjacent="(position()-1) idiv 3"> 
     <Communication> 
      <ChannelCode> 
       <xsl:value-of select="current-group()[1]" /> 
      </ChannelCode> 
      <UseCode> 
       <xsl:value-of select="current-group()[3]" /> 
      </UseCode> 
      <DialNumber> 
       <xsl:value-of select="current-group()[2]" /> 
      </DialNumber> 
     </Communication> 
</xsl:for-each-group> 
+0

而重要的区别是...? –

+0

对不起,我错过了你的答案。不知道如何。 –

+0

感谢您的反馈。 – user7918368