for-each-group文本段落 - xslt 2.0

时间:2015-05-28 13:39:43

标签: html xml xslt xslt-2.0

我正在寻找基于标题h1的组文本解决方案。我用for-each-groups尝试了这个,start-with =" h1"。问题是h1与其他元素(div / h1)的级别不同。

输入html:

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>   
</head>
<body>
<div>
    <h1><b>TRAIN</b></h1>
</div>
<p>text</p>
<p>In this field there is text</p>
<div>
    <h1><b>nr1</b><b>CAR</b></h1>
</div>
<h2><b>1.</b><b>nr2</b><b>area</b></h2>
<p>infos about cars</p>
<p><b>more and</b>more infos about cars</p>
</body>
</html>

到目前为止我所拥有的是:

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

<xsl:template match="head"/>

<xsl:template match="body">

    <xsl:for-each-group select = "*" group-starting-with = "h1">
        <output>
            <xsl:apply-templates select="current-group()"/>
        </output>
    </xsl:for-each-group>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{name()}">
        <xsl:apply-templates select="node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

但是输出并没有按照我想要的方式运行。我希望有两个输出块作为此示例输出:

<html>
<output>
  <div><h1><b>TRAIN</b></h1></div>
  <p>text</p>
  <p>In this field there is text</p>
</output>

<output>
  <div><h1><b>nr1</b><b>CAR</b></h1></div>
  <h2>
     <b>1.</b>
     <b>nr2</b>
     <b>area</b>
  </h2>
  <p>infos about cars</p>
  <p><b>more and</b>more infos about cars</p>
</output>

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以使用descendant-or-self轴,以h1作为后代(或h1元素本身)的元素开始分组。

<xsl:for-each-group select="*" group-starting-with="*[descendant-or-self::h1]">

另请注意,在您的XSLT中,您使用了xpath-default-namespace,但您的输入XML不使用该命名空间,因此您的XSLT中的body模板不会与输入匹配。您需要将默认命名空间添加到输入中,或从XSLT中删除xpath-default-namespace

答案 1 :(得分:1)

怎么样:

XSLT 2.0

1.From Package Manager Console run:

sqllocaldb.exe stop v11.0

sqllocaldb.exe delete v11.0


2.Run your project

3.Register a user