首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从相同的XML元素值字符串中提取句子

从相同的XML元素值字符串中提取句子
EN

Stack Overflow用户
提问于 2021-05-05 09:19:59
回答 1查看 60关注 0票数 0

我希望使用XSL从相同的XML元素值字符串中提取句子。

需求:

每个句子都将在XHTML结果文件段落标记中结束。每个XHTML段落中的句子数量可能会有所不同。控制句子结尾的总是每个点。XML数据可以在不同的行上传播。XML节点中的句子数量可以更大,然后在下面的代码中解释,所以我不能硬编码不同句子的位置。

我试图解决的问题是:

我设法用“子字符串-在”第一个点之前找到第一个句子。

问题:

是否有“最佳实践”如何使用XSL2.0对XML文本字符串语句进行子集,以及如何在下面的代码中应用?

您可以在这里找到以下代码:https://xsltfiddle.liberty-development.net/asoTJE/2

XML

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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结果

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2021-05-05 09:48:54

就像这样:

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67398483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档