将传统逻辑转换为XSLT

时间:2015-08-01 14:10:37

标签: regex xslt

只是尝试将以下简单代码转换为XSLT,我尝试将正则表达式与XSLT 2.0一起使用,但有些方法却无法正常工作。有人可以建议吗?谢谢。

string[256] Fname;
integer ctr;
Fname="";
ctr=0;
while ctr <= len(#FirstName) do
begin
  if mid(#FirstName,ctr,1) = "A" |
     mid(#FirstName,ctr,1) = "B" |
     mid(#FirstName,ctr,1) = "C" |
     mid(#FirstName,ctr,1) = "D" |
     mid(#FirstName,ctr,1) = "E" |
     mid(#FirstName,ctr,1) = "F" |
     mid(#FirstName,ctr,1) = "G" |
     mid(#FirstName,ctr,1) = "H" |
     mid(#FirstName,ctr,1) = "I" |
     mid(#FirstName,ctr,1) = "J" |
     mid(#FirstName,ctr,1) = "K" |
     mid(#FirstName,ctr,1) = "L" |
     mid(#FirstName,ctr,1) = "M" |
     mid(#FirstName,ctr,1) = "N" |
     mid(#FirstName,ctr,1) = "O" |
     mid(#FirstName,ctr,1) = "P" |
     mid(#FirstName,ctr,1) = "Q" |
     mid(#FirstName,ctr,1) = "R" |
     mid(#FirstName,ctr,1) = "S" |
     mid(#FirstName,ctr,1) = "T" |
     mid(#FirstName,ctr,1) = "U" |
     mid(#FirstName,ctr,1) = "V" |
     mid(#FirstName,ctr,1) = "W" |
     mid(#FirstName,ctr,1) = "X" |
     mid(#FirstName,ctr,1) = "Y" |
     mid(#FirstName,ctr,1) = "Z" |
     mid(#FirstName,ctr,1) = "a" |
     mid(#FirstName,ctr,1) = "b" |
     mid(#FirstName,ctr,1) = "c" |
     mid(#FirstName,ctr,1) = "d" |
     mid(#FirstName,ctr,1) = "e" |
     mid(#FirstName,ctr,1) = "f" |
     mid(#FirstName,ctr,1) = "g" |
     mid(#FirstName,ctr,1) = "h" |
     mid(#FirstName,ctr,1) = "i" |
     mid(#FirstName,ctr,1) = "j" |
     mid(#FirstName,ctr,1) = "k" |
     mid(#FirstName,ctr,1) = "l" |
     mid(#FirstName,ctr,1) = "m" |
     mid(#FirstName,ctr,1) = "n" |
     mid(#FirstName,ctr,1) = "o" |
     mid(#FirstName,ctr,1) = "p" |
     mid(#FirstName,ctr,1) = "q" |
     mid(#FirstName,ctr,1) = "r" |
     mid(#FirstName,ctr,1) = "s" |
     mid(#FirstName,ctr,1) = "t" |
     mid(#FirstName,ctr,1) = "u" |
     mid(#FirstName,ctr,1) = "v" |
     mid(#FirstName,ctr,1) = "w" |
     mid(#FirstName,ctr,1) = "x" |
     mid(#FirstName,ctr,1) = "y" |
     mid(#FirstName,ctr,1) = "z" |
     mid(#FirstName,ctr,1) = "-" 
     then
   Fname = Fname + mid(#FirstName,ctr,1);
  ctr = ctr+1;
end

Fname = trimleft(Fname,"-");

if len(Fname) = 2 then
   Fname = Fname + "-";
if len(Fname) = 1 then
   Fname = Fname + "--";

if len(Fname) > 50 then
   Fname = left(Fname,50);

if len(Fname) = 0 then
   Fname = "UNKNOWN";

#FirstName = Fname;

解决方案: 相同的逻辑应用于另一个名为PostalCode的字段,这是我尝试过但有些正则表达式无法正常工作。我正在努力解决这个问题,同时发布在这里以及专家解决方案。

   <xsl:if test="contains(substring(string(/OrdersToFulfill/Order/OrderHeader/BillTo/Address/PostalCode),1,1),'[^a-zA-Z1-9. ]')">
                            <xsl:variable name="vPostalCode"
                                          select="concat(string(/OrdersToFulfill/Order/OrderHeader/BillTo/Address/PostalCode),substring(string(/OrdersToFulfill/Order/OrderHeader/BillTo/Address/PostalCode),1,1))"/>
                            <xsl:element name="PostalCode">
                                <xsl:if test="string-length($vPostalCode) &lt; 9 ">
                                    <xsl:value-of
                                            select="substring($vPostalCode,1, 5)"/>
                                </xsl:if>
                                <xsl:if test="string-length(string(/OrdersToFulfill/Order/OrderHeader/BillTo/Address/PostalCode)) &gt; 10 ">
                                    <xsl:value-of
                                            select="substring($vPostalCode,1, 10)"/>
                                </xsl:if>
                            </xsl:element>
                        </xsl:if>

1 个答案:

答案 0 :(得分:1)

好吧,我想我已经解决了你的代码所做的事情:它构造了一个包含输入字符串中所有字母,数字和连字符的字符串,并删除了其他所有内容。然后用连字符填充至少三个长度,并截断到最大长度为50.(为什么你不能告诉我们这个?)

此外,如果您尝试编写代码并且它不起作用,那么您应该向我们展示代码,以便我们可以告诉您出错的地方。

问题的第一部分可以使用

完成
replace($in, "[^A-Za-z0-9\-]", "")

用连字符填充长度为3可以用

完成
if (string-length($s) lt 3) 
then substring(concat($s, "---"), 1, 3) 
else $s

使用

可以截断最多50个字符
substring($s, 1, 50)