2012-07-18 46 views
1

我有一个XML节点,它是下面一个较大的XML的一部分,它包含下标中的某些符号。带有“小于”符号的XSLT格式字符串

<MT N="Abstract" V="Centre-of-mass energies in the region 142&lt;W&lt;sub&gt;γp&lt;/sub&gt;&lt;293 GeV with the ZEUS detector at HERA using an integrated luminosity"/> 

我需要格式化在@V属性中的值,使得每个&lt;其由字母成功如上&lt;W,应更换为&lt; W,在它们之间具有单一的空间,当与XSLT解析。

这可能吗?首选XSLT 1.0解决方案。

回答

2

这是可能的。在XSLT 2.0中,这将是一个轻而易举(具有正则表达式)。然而,这是直接的“你说什么”脚本在XSLT 1.0:

<xsl:template match="/"> 
    <xsl:call-template name="process"> 
     <xsl:with-param name="text" select="/tutorial/MT/@V"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="process"> 
    <xsl:param name="text" select="."/> 
    <xsl:variable name="modtext" select="translate($text,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')"/> 
    <xsl:variable name="pretext" select="substring-before($modtext,'&lt;a')"/>   
    <xsl:choose> 
     <xsl:when test="not($pretext)"> 
      <xsl:value-of select="$text"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:variable name="endpos" select="string-length($pretext)+1"/> 
      <xsl:value-of select="concat(substring($text,1, $endpos),' ')"/> 
      <xsl:call-template name="process"> 
       <xsl:with-param name="text" 
        select="substring($text,$endpos+1)"/> 
      </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

产生你问什么,但它的表现很奇怪数字和/字符。

它产生:

Centre-of-mass energies in the region 142&lt; W&lt; sub&gt;γp&lt;/sub&gt;&lt;293 GeV with the ZEUS detector at HERA using an integrated luminosity 

显然,如果你更新与翻译/和1234567890它会处理数字和斜线了。

+0

谢谢!它按需要工作。 *我想我的需求非常安全。 – itsbalur 2012-07-18 08:59:33

+1

'*'有时可能是安全的,但为什么要培养一个bug。固定。 – 2012-07-18 09:09:37

0

容易在XSLT 2.0:

replace(@V, '(&lt;)(\p{L})', '$1 $2') 

在XSLT 1.0更难,足够硬了,我没有时间去做这种尝试。

+0

感谢您的意见。我们很快将采用XSLT 2.0,所以这会派上用场。 – itsbalur 2012-07-18 09:00:25