1

我有大量的 html 文件,例如以下 01.html 文件:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>My Title</title> 
  </head>
  <body>
    <item itemprop="itemprop1" content="content1" /> 
    <item itemprop="itemprop2" content="content2" /> 
    <item itemprop="itemprop3" content="content3" /> 
    <item itemprop="itemprop4" content="content4" />
    <item itemprop="itemprop5" content="content5" />
    <item itemprop="itemprop6" content="content6" />
    <item itemprop="itemprop7" content="content7" />
    <item itemprop="itemprop8" content="content8" />
    <item itemprop="itemprop9" content="content9" />
  </body>
</html>

每个 html 文件中只有一个 itemprop="itemprop1" 的 item 节点。itemprop2、itemprop3 等也是如此。

我想要以下 txt 文件输出:

content1 | content 5

即以下内容的连接: 1. itemprop="itemprop1" 的项目的属性内容的值 2. 管道“|” 3. itemprop="itemprop5" 的item的属性content的值

我运行以下 bash 脚本:

xsltproc 01.xslt 01.html >> 02.txt

其中 01.xslt 如下:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="body">
  <xsl:value-of select="//item[@itemprop='itemprop1']/@content"/>|<xsl:value-of select="item[@itemprop='itemprop5']/@content"/>
 </xsl:template>

</xsl:stylesheet>

不幸的是,它不起作用。什么是正确的 xslt 文件?

更新

这是最后一个工作示例。

01.html如下:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>My Title</title> 
  </head>
  <body>
    <item itemprop="itemprop1" content="content1" /> 
    <item itemprop="itemprop2" content="content2" /> 
    <item itemprop="itemprop3" content="content3" /> 
    <item itemprop="itemprop4" content="content4" />
    <item itemprop="itemprop5" content="content5" />
    <item itemprop="itemprop6" content="content6" />
    <item itemprop="itemprop7" content="content7" />
    <item itemprop="itemprop8" content="content8" />
    <item itemprop="itemprop9" content="content9" />
  </body>
</html>

01.xslt 如下:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes" method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="html">
  <xsl:value-of select="//item[@itemprop='itemprop1']/@content"/>
  <xsl:text>|</xsl:text>
  <xsl:value-of select="//item[@itemprop='itemprop5']/@content"/>
 </xsl:template>

</xsl:stylesheet>

输出 02.txt 如下:

content1|content5
4

3 回答 3

2

实际上,XSTL 处理的是XML文件,而不是HTML

您的源 HTML几乎满足格式良好的 XML 的要求。只有一个错误:您的meta元素没有关闭,所以我将其更改为:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

/在关闭之前添加>)。否则 XSLT 处理器会显示错误消息(至少在我的安装中)。

就您的 XSLT 而言,我做了一些更正:

  • match="body"改为match="html",
  • //在第二个中添加xsl:value-of
  • 将“bare”更改|<xsl:text>|</xsl:text>,仅出于可读性原因(在较小的显示器上看不到较长的行),
  • 添加<xsl:output method="text"/>为您的输出似乎不是任何 XML。

最后 2 个更改是可选的,您可以忽略它们。

所以整个脚本可以如下所示:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="html">
    <xsl:value-of select="//item[@itemprop='itemprop1']/@content"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="//item[@itemprop='itemprop5']/@content"/>
  </xsl:template>
</xsl:stylesheet>
于 2018-06-25T19:07:10.663 回答
1

您使用的主要问题xsltproc是您正在尝试处理 HTML 而不是 XML。不同之处<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">在于未关闭的标记,因此 XSLT 处理器没有有效的 XML(这会导致错误)。所以添加一个结束字符来使它

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

如果您解决此问题并添加一个模板来删除“不匹配”text()节点,例如

<xsl:template match="text()" />

你的 XSLT 会做你想做的事。

于 2018-06-25T19:08:24.393 回答
0
<xsl:output method="text" indent="yes"/>
    <xsl:template match="/">
        <xsl:value-of select="html/body/item[@itemprop='itemprop1']/@content"/>|<xsl:value-of select="html/body/item[@itemprop='itemprop5']/@content"/>
    </xsl:template>
于 2018-06-26T05:32:38.313 回答