我的 xml 文件看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" content="text/html" http-equiv="Content-Type"/>
</head>
<body>
<div> </div>
Hello body content !!
</body>
</html>
</xsl:template>
<xsl:template name="br-replace">
<xsl:param name="word"/>
<xsl:choose>
<xsl:when test="contains($word,'
')">
<xsl:value-of select="substring-before($word,'
')"/>
<br xmlns="http://www.w3.org/1999/xhtml"/>
<xsl:call-template name="br-replace">
<xsl:with-param name="word" select="substring-after($word,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$word"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="format-date">
<xsl:param name="word"/>
<xsl:value-of select="substring($word, 1, 10)"/>
</xsl:template>
</stylesheet>
我正在尝试将其分为三个部分:
- 之前的文字
<body>
- 之间的文字
<body> and </body>
- 之后的文字
</body>
Java代码:
Matcher before = Pattern.compile("(.*?)<body>", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
.matcher(input);
String beforeStr = null;
if (before.find()) {
beforeStr = before.group(1);
}
Matcher after = Pattern.compile("</body>(.*?)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
.matcher(input);
String afterStr = null;
if (after.find()) {
afterStr = after.group(1);
}
Matcher body = Pattern.compile("<body>(.*?)</body>",
Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(input);
String bodyStr = null;
if (body.find()) {
bodyStr= body.group(1);
}
知道为什么 String 'afterStr' 为空,模式有问题吗?