某些条件下的 Xslt [英] Xslt under some conditions
本文介绍了某些条件下的 Xslt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 xml 和 xslt 的新手.我有一些以某些值的形式存在的数据.
I am new to xml and xslt. I have some data which is in the form of some values.
假设这是一个 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 遇到三个值
So this means whenever xslt comes accross the three values
517-98-0045
449-235-1235
4265-6857-1293-0098
它必须把它变成这个
517-XX-XXXX
449-XXX-XXXX
4265-XXXX-XXXX-XXXX
我知道正则表达式可以做到这一点,但我该怎么做呢?
I know that regular expressions can accomplish this, but how can I do that?
根据下面给出的答案..以下是否正确---
According to the undergiven answers..Is this following correct---
<?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>
推荐答案
如果您只想替换这三个特定值,那么应该这样做:
If you just want to replace those three particular values, then this should do it:
<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>
这篇关于某些条件下的 Xslt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文