【发布时间】:2017-12-22 15:43:47
【问题描述】:
我需要获取每个“/”的值,但由于文件中包含特殊字符,我无法获得正确的输出。如何在检查中使用正确的正则表达式?我正在使用 <xsl:analyze-string> 元素来获取值。这是我的示例文件:
输入文件:
<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> 中分配第三个。
提前感谢您的反馈。
【问题讨论】: