Xslt在某些条件下

时间:2014-03-17 05:56:36

标签: xml regex xslt

我是xml和xslt的新手。我有一些数据形式的数据。

假设这是一个xml

<root>
    <z>517-98-0045</z>
    <z1>449-235-1235</z1>
    <z2>4265-6857-1293-0098</z2>
    <z3>3232-3-3232-3232-12</z3>
    <z4>449-235-1235</z4>
    <z5>332323-32-322</z5>
    <z6>218-28-2332</z6>
    <z7>517-98-0045</z7>
</root>

应更改为以下内容:

<z>517-xx-xxxx</z>
<z1>449-xxx-xxxx</z1>
<z2>4265-xxxx-xxxx-xxxx</z2>
<z3>3232-3-3232-3232-12</z3>
<z4>449-xxx-xxxx</z4>
<z5>332323-32-322</z5>
<z6>218-28-2332</z6>
<z7>517-xx-xxxx</z7>

所以这意味着只要xslt来自三个值

517-98-0045
449-235-1235
4265-6857-1293-0098

它必须将其转换为此

517-XX-XXXX
449-XXX-XXXX
4265-XXXX-XXXX-XXXX

我知道正则表达式可以实现这一点,但我该怎么做呢?

根据被忽视的答案......这是正确的---

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
  <xsl:template match="/">
<xsl:when test="matches(517-98-0045,'\d{3}-\d{2}-\d{4}')">
    <xsl:value-of select="replace('517-98-0045','517-XX-XXXX');"/>
  </xsl:when>
<xsl:when test="matches(449-235-1235,'\d{3}-\d{3}-\d{4}')">
    <xsl:value-of select="replace('449-235-1235','449-XX-XXXX');"/>
  </xsl:when>
<xsl:when test="matches(4265-6857-1293-0098,'\d{4}-\d{4}-\d{4}-\d{4}')">
    <xsl:value-of select="replace('4265-6857-1293-0098','4265-XXxx-XXXX-xxxx');"/>
  </xsl:when>
<xsl:template>
<xsl:stylesheet>

3 个答案:

答案 0 :(得分:1)

如果您只想替换这三个特定值,那么应该这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()[. = '517-98-0045' or
                              . = '449-235-1235' or
                              . = '4265-6857-1293-0098']">
    <xsl:value-of select="concat(substring-before(., '-'), 
                                 '-',
                                 translate(substring-after(., '-'),
                                           '0123456789', 'XXXXXXXXXX'))"/>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

以下3个正则表达式应该可以解决问题:

replace('517-98-0045','(\d{3})-(\d{2})-(\d{4})','$1-XX-XXXX');
replace('449-235-1235','(\d{3})-(\d{3})-(\d{4})','$1-XXX-XXXX');
replace('4265-6857-1293-0098','(\d{4})-(\d{4})-(\d{4})-(\d{4})','$1-XXXX-XXXX-XXXX-XXXX');

现在唯一需要检查哪个替换功能适用。函数匹配可以做到这一点:

<xsl:choose>
  <xsl:when test="matches(.,'\d{3}-\d{2}-\d{4}')">
    <xsl:value-of select="replace(.,'(\d{3})-(\d{2})-(\d{4})','$1-XX-XXXX');"/>
  </xsl:when>
  ...

答案 2 :(得分:0)

如果始终是替换为'X'的第一组数字,则下面的解决方案有效。

我认为你不需要比这更复杂的正则表达式。

示例输入XML (因为您没有显示任何XML输入)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <n>517-98-0045</n>
  <n>449-235-1235</n>
  <n>4265-6857-1293-0098</n> 
</root>

<强>样式表

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/root">
  <xsl:copy>
      <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="n">
    <xsl:copy>
        <xsl:value-of select="concat(substring-before(.,'-'),'-',replace(substring-after(.,'-'),'\d','X'))"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

<强>输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <n>517-XX-XXXX</n>
  <n>449-XXX-XXXX</n>
  <n>4265-XXXX-XXXX-XXXX</n> 
</root>

编辑(您使用实际的XML输入更新了问题)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/root|root/*">
  <xsl:copy>
      <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text()[.='4265-6857-1293-0098' or .='449-235-1235' or .='517-98-0045']">
    <xsl:value-of select="concat(substring-before(.,'-'),'-',replace(substring-after(.,'-'),'\d','X'))"/>
</xsl:template>

<xsl:template match="text()">
    <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>