如何从一个xhtml文档中提取一个div部分到另一个xhtml文档 [英] How to extract a div section from one xhtml document into another xhtml document

查看:151
本文介绍了如何从一个xhtml文档中提取一个div部分到另一个xhtml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用xslt从xhtml文档中提取一个div部分到另一个xhtml文档。但是,我没有成功。相反,xslt变换导致有线输出。假设要转换的以下xhtml文档:

I'm trying to extract a div section from an xhtml document into another xhtml document by using xslt. However, I did not succeed. Rather, the xslt transformation resulted in a wired output. Suppose the following xhtml document to transform:

<?xml version="1.0" encoding="iso-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head>
   <title>title test</title>
</head>
<body>
   some blabla
   <div>
      <div id="testid" class="testclass">
         hello world!
      </div>
   </div>
   some other blabla <p/>
   test paragraph<p/>
</body>
</html>

xslt应提取id为testid的div部分,并将其写入新的xhtml文档其格式如下:

The xslt should extract the div section with the id "testid" and write it into a new xhtml document which should look as follows:

<?xml version="1.0" encoding="iso-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head>
   <title>title test</title>
</head>
<body>
   <div id="testid" class="testclass">
      hello world!
   </div>
</body>
</html>

我的xslt代码如下所示:

My xslt code looks as follows:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="de">
      <head>
      </head>
      <body>
        <xsl:apply-templates select="node()|text()"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="*">
    <xsl:if test="div[@id='testid']">
      <xsl:copy-of select="*"/>
    </xsl:if>
    <xsl:apply-templates select="node()|@*" />
  </xsl:template>

</xsl:stylesheet>

实际输出如下:

<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
  <head/>
  <body>de

    title test

    some blabla


    testidtestclass
    hello world!


    some other blabla
    test paragraph

  </body>
</html>

为了获得正确的结果,需要在xslt中进行哪些更改?任何帮助是赞赏。感谢。

What do I need to change in the xslt in order to get the correct results? Any help is appreciated. Thanks.

推荐答案

此样式表(拉式样):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xhtml="http://www.w3.org/1999/xhtml"
 xmlns="http://www.w3.org/1999/xhtml"
 exclude-result-prefixes="xhtml">
    <xsl:template match="text()"/>
    <xsl:template match="xhtml:div[@id='testid']">
        <html lang="de">
            <head></head>
            <body>
                <xsl:call-template name="identity"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="node()|@*" mode="identity" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" mode="identity"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

使用此输入:

<?xml version="1.0" encoding="iso-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
    <head>
        <title>title test</title>
    </head>
    <body>some blabla
        <div>
            <div id="testid" class="testclass">hello world!</div>
        </div>some other blabla 
        <p>test paragraph</p>
    </body>
</html>

输出:

<html lang="de" xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body>
        <div id="testid" class="testclass">hello world!</div>
    </body>
</html>

注意:匹配节点的发票身份规则。

Note: Invoque identity rule from matching node.

此样式表(推式):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xhtml="http://www.w3.org/1999/xhtml"
 xmlns="http://www.w3.org/1999/xhtml"
 exclude-result-prefixes="xhtml">
    <xsl:template match="/">
        <html lang="de">
            <head></head>
            <body>
                <xsl:apply-templates select="//xhtml:div[@id='testid']"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

和brick模板:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xhtml="http://www.w3.org/1999/xhtml"
 xmlns="http://www.w3.org/1999/xhtml"
 exclude-result-prefixes="xhtml">
    <xsl:template match="/">
        <html lang="de">
            <head></head>
            <body>
                <xsl:copy-of select="//xhtml:div[@id='testid']"/>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

这篇关于如何从一个xhtml文档中提取一个div部分到另一个xhtml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆