<Description>this is my value 822880494 this is my value</Description>
我是很新的XPath,XML和stylevision所以这可能是一个基本问题。
我使用stylevision 2010和XPath创建一个架构中的SPS / XSLT。
在上面的节点,可以看到有节点内的数值,我想提取价值,把它变成我的PDF / HTML的链接。问题是,我似乎无法解压缩。子是不是因为该值的长度,并在其内部不同的数值的位置的选项。
有些可能会认为,该模式被严重组成,并且该数值应该在一个单独的节点/属性/ ... ...有什么我可以做的,因为该模式是由另一家公司提供的。
提前致谢!
StyleVision 2010似乎支持XSLT 2.0,所以你可以使用一个2.0样式表,并做一些像
<xsl:analyze-string select='$foo' regex='\d+'>
<xsl:matching-substring>
<number><xsl:value-of select='.' /></number>
</xsl:matching-substring>
</xsl:analyze-string>
或任何你想用数字做;用数字字符串是<xsl:matching-substring>
元素内的上下文元素。
(对于XSLT 1.0)Newtover的translate
想法是这样的:
<xsl:value-of select="translate(., translate(., '0123456789', ''), '')" />
但是,如果你的输入包含多个号码,也只会将它们连接起来。
使用这个简单的XPath 1.0表达式:
translate(.,translate(., '0123456789', ''), '')
这是一个完整的XSLT 1.0解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:value-of select=
"translate(.,translate(., '0123456789', ''), '')"/>
</xsl:template>
</xsl:stylesheet>
当这种转化应用所提供的XML文档:
<Description>this is my value 822880494 this is my value</Description>
在想,正确的结果产生:
822880494
说明:
这被称为双翻译方法首先由迈克尔·凯建议。它包括两个嵌套调用的函数translate()
:
translate()
。这将产生字符串的所有字符,除了数字。translate()
。这将删除从字符串由内translate()
产生的所有字符。剩下的才是有用的字符(数字)。纯XSLT 1.0脆弱的,但可能的解决方案是使用translate
的成分(使所有非数值为空字符串或空格)和normalize-space
(修剪的空间休息,虽然translate
可能足够了)。这无疑将只工作,如果有在字符串中没有其他的数值。而且,我目前无法查询,translate
可能只工作,如果你的字符串包含ASCII字符。
XSLT 2.0有几个正则表达式的功能。如果您的XSLT处理器允许使用EXSLT的一些推广,它也包含了正则表达式功能,或者您也可以通过空格标记化的字符串,并提供非空的模板,只有数字令牌。
附:我很抱歉,我不提供任何的联系,很难从设备到。
嗨,这将产生您需要的信息!它会检查每个字符,然后确保它是一个数字。
XSLT 1个解决方案
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Root/Description">
<xsl:call-template name="for-each-character">
<xsl:with-param name="data" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="for-each-character">
<xsl:param name="data"/>
<xsl:if test="string-length($data) > 0">
<xsl:if test="substring($data,1,1)>-1">
<xsl:value-of select="substring($data,1,1)"/>
</xsl:if>
<xsl:call-template name="for-each-character">
<xsl:with-param name="data" select="substring($data,2)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
以下是上述XSLT V1溶液的推导,然而,这是专门为前导号码,如与嵌入在字符串的中间。它还允许浮点或整数解析。 (I亲自找到用于从值分裂开单位的此有用的,如“80毫克”或“128.4平方毫米”,其中单位是“毫米2”,和值“128.4”,而不是“128.42”。
<xsl:template name="parseNumber">
<xsl:param name="data"/>
<xsl:param name="is-float" select="false()"/><!-- has this already been determined to be a non-integer -->
<xsl:if test="string-length($data) > 0">
<xsl:if test="(substring($data,1,1)>-1) or ((substring($data,1,1) = '.') and (not($is-float)) )">
<xsl:value-of select="substring($data,1,1)"/>
<xsl:call-template name="parseNumber">
<xsl:with-param name="data" select="substring($data,2)"/>
<xsl:with-param name="is-float" select="(substring($data,1,1) = '.') or ($is-float)"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
以下是一些单元测试例比较的结果:
Test: [123] ?=? numer(): [123] ?=? for-each-char: [123] ?=? parseNumber: [123]
Test: [1.23] ?=? numer(): [1.23] ?=? for-each-char: [1.23] ?=? parseNumber: [1.23]
Test: [1.1.1.1] ?=? numer(): [NaN] ?=? for-each-char: [1.1.1.1] ?=? parseNumber: [1.1]
Test: [123 abc] ?=? numer(): [NaN] ?=? for-each-char: [123] ?=? parseNumber: [123]
Test: [123 abc2] ?=? numer(): [NaN] ?=? for-each-char: [1232] ?=? parseNumber: [123]
Test: [123.456 abc7] ?=? numer(): [NaN] ?=? for-each-char: [123.4567] ?=? parseNumber: [123.456]
Test: [abc def ] ?=? numer(): [NaN] ?=? for-each-char: [] ?=? parseNumber: []
Test: [abc 123] ?=? numer(): [NaN] ?=? for-each-char: [123] ?=? parseNumber: []