如何使用XSLT v1.0插入文本而不是使用XSLT v2.0正则表达式? [英] How to insert text with XSLT v1.0 instead of using an XSLT v2.0 regex?

查看:79
本文介绍了如何使用XSLT v1.0插入文本而不是使用XSLT v2.0正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,它描述(除其他外)具有描述完全限定的java类名的属性值的元素。我正在尝试编写一个XSLT转换来修改此文件中描述的类名,这样(例如) com.example.MyClass 的ocurrances将变为 com.example.MockMyClass

I have an xml file which describes (among other things) elements with attribute values that describe fully qualified java class names. I am trying to write an XSLT transformation to modify the class names described in this file, such that (for example) ocurrances of com.example.MyClass will become com.example.MockMyClass.

以下是原始文件片段上下文中的示例:

Here's that example again in the context of a fragment of the original file:

<event type="node-enter">
  <action name="MyActionName" class="com.example.MyClass">
    <bodyTemplate>
      templates/MyTemplate.vm
    </bodyTemplate>
  </action>
</event>

我希望结果为:

<event type="node-enter">
  <action name="MyActionName" class="com.example.MockMyClass">
    <bodyTemplate>
      templates/MyTemplate.vm
    </bodyTemplate>
  </action>
</event>

我正在使用Java JAXP API进行此转换,并编写了一个可爱的符合XSLT 2.0的正则表达式例程来获取我想要的结果,只发现Java 5不支持XSLT 2.0,这是正则表达式支持所必需的。

I'm doing this transformation using the Java JAXP API, and had written a lovely XSLT 2.0 compliant regex routine to get the results I want, only to discover that Java 5 doesn't support XSLT 2.0, which is required for regex support.

所以我的问题是,什么是使用古老的JAXP XSLT 1.0 API实现这一目标的最佳方法是什么?也就是说,不使用正则表达式。我寻找类似的问题,但反向引用正则表达式组的要求似乎使这个问题变得棘手。 这个问题是一个开始,但我需要在匹配的字符串中插入文本,而不仅仅是替换

So my question is, what is the best way to achieve this using the archaic JAXP XSLT 1.0 API? That is, without the use of regular expressions. I looked for similar problems, but the requirement for backreferencing regex groups seems to make this a tricky one. This question is a start, but I need to insert text, within a matching string, rather than just replacing.

作为参考,这是我的正则表达式(XSLT 2.0)尝试:

For reference, here is my regex (XSLT 2.0) attempt:

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  <xsl:template match='/'>
    <xsl:analyze-string select='action/@class' regex='([A-Za-z0-9]+[$\.])+([A-Za-z0-9]+)'>
      <xsl:matching-substring>
        <xsl:value-of select='regex-group(1)'/>
        <xsl:text>Mock</xsl:text>
        <xsl:value-of select='regex-group(2)'/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select='.'/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>
</xsl:stylesheet>


推荐答案

以下情况如何?

<xsl:template name="classname">
    <xsl:param name="class"/>
    <xsl:choose>
        <xsl:when test="contains($class,'.')">
            <xsl:value-of select="concat(substring-before($class,'.'),'.')"/>
            <xsl:call-template name="classname">
                <xsl:with-param name="class"
                                    select="substring-after($class,'.')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="concat('Mock',$class)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

这会将一个类名作为输入参数,并在最后的。之后添加Mock。您可以使用,例如,

This takes a classname as an input parameter and adds "Mock" after the final ".". You can call it with, for example,

 <xsl:call-template name="classname">
     <xsl:with-param name="class" select="@class"/>
 </xsl:call-template>

(我刚刚在Firefox中试了一下,你可能会发现你需要做一些整理白色空间。)

(I just gave it a quick try in Firefox, you might find you need to do some tidying up of white space.)

这篇关于如何使用XSLT v1.0插入文本而不是使用XSLT v2.0正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆