xml - XSLT 合并节点

标签 xml xslt xhtml

所以我有一个凌乱的 xhtml 文件,我想将其转换为 xml。这是一个带有很多“p”标签的词典,我想把它们整理出来。这是 xhtml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta content="2018-06-29T10:12:48Z" name="dcterms.created" />
        <meta content="2018-06-29T10:12:48Z" name="dcterms.modified" />
    </head>
    <body>
        <p><b>Aesthetik</b></p>
        <p>text about aesthetics.</p>
        <p><b>Expl: </b>explanation about aesthetics</p>
        <p><b>BegrG: </b>origin of the term</p>
        <p>more origin of the term</p>
        <p><b>Allegorese</b></p>
        <p>text about Allegorese</p>
        <p><b>Expl: </b>explanation about Allegorese</p>
        <p><b>BegrG: </b>origin of Allegorese</p>
    </body>
</html>

XSLT 文件如下所示(其他标记还有几行,此处未包括在内):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://www.w3.org/1999/xhtml">

<xsl:template match="head"/>

<xsl:template match="text()">
    <xsl:value-of select="normalize-space()"/>
</xsl:template>

<xsl:template match="body">
    <lexica>
        <xsl:apply-templates/>      <!-- create root node lexica -->
    </lexica>
</xsl:template>

<xsl:template match="p">
    <p>
        <xsl:apply-templates/> <!-- copy same tags for better visuality -->
    </p>
</xsl:template>

<xsl:template match="p[b[contains(., 'BegrG')]]">
    <BegrG>
        <xsl:apply-templates/>  <!-- create specific nodes with origin explanation of the word -->
    </BegrG>
</xsl:template>

<xsl:template match="p[b[contains(., 'Expl')]]">
    <Expl>
        <xsl:apply-templates/>  <!-- node with explanation of the word --> 
    </Expl>
</xsl:template>


<xsl:template
    match="
    p[b[not(self::*[contains(., 'Expl')]or
    self::*[contains(., 'BegrG')])]]">  <!-- any other b nodes which are left are lexical items -->
    <Artikel>
        <xsl:apply-templates/>
    </Artikel>
</xsl:template>

最后我的 XML 文件看起来像这样:

    <lexica>
    <Artikel>Aesthetik</Artikel>
    <p>text about aesthetics.</p>
    <Expl>Expl:explanation about aesthetics</Expl>
    <BegrG>BegrG:origin of the term</BegrG>
    <p>more origin of the term</p>
    <Artikel>Allegorese</Artikel>
    <p>text about Allegorese</p>
    <Expl>Expl:explanation about Allegorese</Expl>
    <BegrG>BegrG:origin of Allegorese</BegrG>
</lexica>

它看起来更好但仍然无法工作,因为它的结构不够。例如,术语没有分组,一些“p”标签应该合并到它们之前的兄弟标签中。它应该是这样的:

<lexica>
 <item>
  <Artikel>Aesthetik</Artikel>
  <short>text about aesthetics.</short>
  <Expl>Expl:explanation about aesthetics</Expl>
  <BegrG>BegrG:origin of the term. more origin of the term.</BegrG>
 </item>

 <item>
  <Artikel>Allegorese</Artikel>
  <short>text about Allegorese</short>
  <Expl>Expl:explanation about Allegorese</Expl>
  <BegrG>BegrG:origin of Allegorese</BegrG>
 </item>
</lexica>

我是不是处理错了,或者我应该如何将“p”标签分组到他们有 b- child 的 sibling ?我如何将术语项彼此分开并使其识别何时应该出现关闭标记?

(抱歉我的英语不好)

提前致谢!

最佳答案

XSLT 2/3 具有 for-each-group group-starting-with ( https://www.w3.org/TR/xslt20/#xsl-for-each-group ),因此您可以使用

  <xsl:template match="body">
      <lexica>
          <xsl:for-each-group select="*" group-starting-with="p[b[not(matches(., '^(Expl|BegrG):'))]]">
              <item>
                  <xsl:apply-templates select="current-group()"/>
              </item>
          </xsl:for-each-group>
      </lexica>
  </xsl:template>

我想,例子在 https://xsltfiddle.liberty-development.net/bFDb2CG .

到目前为止,我还不确定是什么决定了将某些 p 元素合并到 BegrG 结果中,也许是嵌套分组 with

  <xsl:template match="body">
      <lexica>
          <xsl:for-each-group select="*" group-starting-with="p[b[not(matches(., '^(Expl|BegrG):'))]]">
              <item>
                  <xsl:for-each-group select="current-group()" group-starting-with="p[b[starts-with(., 'BegrG:')]]">
                      <xsl:choose>
                          <xsl:when test="self::p[b[starts-with(., 'BegrG:')]]">
                              <BegrG>
                                  <xsl:apply-templates select="current-group()/node()"/>
                              </BegrG>
                          </xsl:when>
                          <xsl:otherwise>
                              <xsl:apply-templates select="current-group()"/>
                          </xsl:otherwise>
                      </xsl:choose>
                  </xsl:for-each-group>
              </item>
          </xsl:for-each-group>
      </lexica>
  </xsl:template>

实现:https://xsltfiddle.liberty-development.net/bFDb2CG/1

至于评论中提出的问题,您可以在group-starting-with中添加另一个匹配项:

  <xsl:template match="body">
      <lexica>
          <xsl:for-each-group select="*" group-starting-with="p[b[not(matches(., '^(Expl|BegrG):'))]]">
              <item>
                  <xsl:for-each-group select="current-group()" group-starting-with="p[b[starts-with(., 'Expl:')]] | p[b[starts-with(., 'BegrG:')]]">
                      <xsl:choose>
                        <xsl:when test="self::p[b[starts-with(., 'Expl:')]]">
                              <Expl>
                                  <xsl:apply-templates select="current-group()/node()"/>
                              </Expl>
                          </xsl:when>
                          <xsl:when test="self::p[b[starts-with(., 'BegrG:')]]">
                              <BegrG>
                                  <xsl:apply-templates select="current-group()/node()"/>
                              </BegrG>
                          </xsl:when>
                          <xsl:otherwise>
                              <xsl:apply-templates select="current-group()"/>
                          </xsl:otherwise>
                      </xsl:choose>
                  </xsl:for-each-group>
              </item>
          </xsl:for-each-group>
      </lexica>
  </xsl:template>

https://xsltfiddle.liberty-development.net/bFDb2CG/2

关于xml - XSLT 合并节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52246768/

相关文章:

xml - 使用 R 对房地产广告进行网络抓取

java - 如何从 xml 中保存换行符?

xml - XSLT - 测试多个兄弟节点,如果有任何兄弟节点与测试匹配,则返回 true

xml - 使用 XSLT 将包装元素放在 XML 标签周围

javascript - 从表单中获取用户输入执行计算(操作顺序)并输出回该表单

xml - 从 Linux 发送 SOAP 请求时出现意外字符

java - 请求时间表达式(XML 语法)%= %

c# - 如何检查标签是否在xslt中包含特定字符串

css - 使用 CSS 交换文本和图像顺序

HTML、XHTML 验证错误 - 无法解决