[英]create and return a Nodelist after parsing a string with regex
I am attempting to make a java function to use in my stylesheet that will split a string based on a regex character and return a NodeList. 我试图在我的样式表中使用java函数,该函数表将基于正则表达式字符拆分字符串并返回NodeList。
For example, I would like my stylesheet to be something like: 例如,我希望我的样式表是这样的:
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
version="1.0">
<xsl:template match="/">
<Root>
<xsl:copy-of select="re:splitRegex('This is my string', '\s\s+')"/>
</Root>
</xsl:template>
</xsl:stylesheet>
And I want my resulting XML to look like this: 我希望我生成的XML看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:stash="xalan://com.nxtech.util.io.Stash">
<token>This</token>
<token>is</token>
<token>my</token>
<token>string</token>
</Root>
I have this function in my java code that returns an array of strings: 我在我的java代码中有这个函数,它返回一个字符串数组:
public static String[] split(String toSplit,String regex) {
return toSplit.split(regex);
}
But I need something that returns a NodeList
, or whatever will format my XML file correctly. 但我需要一些返回
NodeList
东西,或者正确格式化我的XML文件的东西。
I agree with Martin, that it is better to do it in XSLT 2.0 . 我同意Martin的看法,最好在XSLT 2.0中做到这一点。
But if you prefer the Xalan / Java approach, then: 但如果您更喜欢Xalan / Java方法,那么:
Change your Java function, so that it returns concatenated tokens, "enveloped" between token
tags (opening and closing). 更改您的Java函数,以便它返回连接的标记,在
token
标记之间“打开”(打开和关闭)。
`<token>...</token>`
(plus \\n
after each </token>
). (加上每个
</token>
之后的\\n
)。
The returned content should be written out between opening and closing Root tags. 返回的内容应该在打开和关闭Root标记之间写出。
If you for some other reason need a node-set
, you can do it as follows: 如果由于某些其他原因需要
node-set
,则可以按如下方式执行:
In the stylesheet
tag include: 在
stylesheet
标签中包括:
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
Your splitting function should return one concatenated string (as above). 您的拆分函数应该返回一个连接的字符串(如上所示)。
Then, in the proper place write: 然后,在适当的地方写:
<xsl:variable name="wrk">
<xsl:value-of select="re:splitRegex('This is my string', '\s\s+')"/>
</xsl:variable>
<xsl:variable name="tokens" select="exsl:node-set($wrk)"/>
The node-set
function changes a Result Tree Fragment into a "proper" node-set
which you can use eg in XPaths or in any other way. node-set
功能将结果树片段更改为“适当的” node-set
,您可以在XPath中或以任何其他方式使用该node-set
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.