我希望使用XSL从相同的XML元素值字符串中提取句子。
需求:
每个句子都将在XHTML结果文件段落标记中结束。每个XHTML段落中的句子数量可能会有所不同。控制句子结尾的总是每个点。XML数据可以在不同的行上传播。XML节点中的句子数量可以更大,然后在下面的代码中解释,所以我不能硬编码不同句子的位置。
我试图解决的问题是:
我设法用“子字符串-在”第一个点之前找到第一个句子。
问题:
是否有“最佳实践”如何使用XSL2.0对XML文本字符串语句进行子集,以及如何在下面的代码中应用?
您可以在这里找到以下代码:https://xsltfiddle.liberty-development.net/asoTJE/2
XML
<?xml version="1.0" encoding="utf-8" ?>
<library>
<books>
<chapters>
<chapterOne>
A piece of text. Another string.
Something, else here.
</chapterOne>
</chapters>
</books>
</library>XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="http://www.example.org/1"
>
<xsl:output method="xhtml" indent="yes" html-version="5"/>
<xsl:template match="/library/books/chapters/chapterOne">
<html>
<head><title>MyTitle</title></head>
<body>
<!-- Works -->
<p>
Sentence-1. First sentence (in same chapter):
Cut after found first dot.
<xsl:value-of select="substring-before(., '.')" />.
</p>
<!-- Not yet found a way to solve -->
<p>
Sentence-3 (in same chapter):
<xsl:value-of select="." />
</p>
<!-- Not yet found a way to solve -->
<p>
Extracted sentences, e.g. sentence-1 and sentence-2 (in same chapter):
Cut after second dot.
<xsl:value-of select="." />
</p>
<!-- Not yet found a way to solve -->
<p>
Extracted sentences, e.g. sentence-1 and sentence-3 (in same chapter):
<xsl:value-of select="." />
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>XHTML结果
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.example.org/1">
<head>
<title>MyTitle</title>
</head>
<body>
<p>
Sentence-1. First sentence (in same chapter):
Cut after found first dot.
A piece of text.
</p>
<p>
Sentence-3 (in same chapter):
A piece of text. Another string.
Something, else here.
</p>
<p>
Extracted sentences, e.g. sentence-1 and sentence-2 (in same chapter):
Cut after second dot.
A piece of text. Another string.
Something, else here.
</p>
<p>
Extracted sentences, e.g. sentence-1 and sentence-3 (in same chapter):
A piece of text. Another string.
Something, else here.
</p>
</body>
</html>发布于 2021-05-05 09:48:54
就像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="http://www.example.org/1"
>
<xsl:output method="xhtml" indent="yes" html-version="5"/>
<xsl:template match="/library/books/chapters/chapterOne">
<html>
<head><title>MyTitle</title></head>
<body>
<xsl:for-each select="tokenize(.,'[.]')">
<xsl:if test="position()!=last()">
<p>
<xsl:value-of select="." />.
</p>
</xsl:if>
</xsl:for-each>
<!-- Works -->
</body>
</html>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/67398483
复制相似问题