【问题标题】:XSLT Format string with "less than" symbol带有“小于”符号的 XSLT 格式字符串
【发布时间】:2012-07-18 06:51:41
【问题描述】:

我有一个 XML 节点,它是如下较大 XML 的一部分,其中包含下标中的某些符号。

<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 属性中的值,这样每个&amp;lt; 后面都是&amp;lt;W 中的字母,应该替换为&amp;lt; W,它们之间有一个空格,当用 XSLT 解析。

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

【问题讨论】:

    标签: xml string xslt


    【解决方案1】:

    这是可能的。在 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 更新翻译,它也会处理数字和斜杠。

    【讨论】:

    • 谢谢!它可以按需要工作。 * 我猜对我的需求来说是相当安全的。
    • * 有时可能是安全的,但为什么要培养一个错误。固定。
    【解决方案2】:

    在 XSLT 2.0 中很容易:

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

    在 XSLT 1.0 中难度更大,我没有时间尝试。

    【讨论】:

    • 感谢您的意见。我们很快就会采用 XSLT 2.0,所以它会派上用场。
    猜你喜欢
    • 1970-01-01
    • 2020-05-11
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多