【发布时间】:2015-02-26 17:22:38
【问题描述】:
我有一个非常非结构化的 XML 文档(取自 Pandoc 转换的 docx 到 docbook 格式),我正在尝试使用 XSLT 清理它。 xml的格式是这样的;
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<article>
<articleinfo>
<title></title>
</articleinfo>
<informaltable>
<tgroup cols="2">
<colspec align="left" />
<colspec align="left" />
<thead>
<row>
<entry>
<emphasis role="strong">How did you assist
Customer?</emphasis>
</entry>
<entry>
<emphasis>Lorem ipsum dolor sit amet.</emphasis>
</entry>
</row>
</thead>
<tbody>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
<emphasis role="strong">What difference did this make for the
Customer?</emphasis>
</entry>
<entry>
<emphasis>Lorem ipsum dolor sit amet.</emphasis>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
Staff Member: John Smith
</para>
<informaltable>
<tgroup cols="2">
<colspec align="left" />
<colspec align="left" />
<thead>
<row>
<entry>
<emphasis role="strong">How did you assist
Customer?</emphasis>
</entry>
<entry>
<emphasis>Lorem ipsum dolor sit amet.</emphasis>
</entry>
</row>
</thead>
<tbody>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
<emphasis role="strong">What difference did this make for the
Customer?</emphasis>
</entry>
<entry>
<emphasis>Lorem ipsum dolor sit amet.</emphasis>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
Staff Member: John Smith
</para>
<informaltable>
<tgroup cols="2">
<colspec align="left" />
<colspec align="left" />
<thead>
<row>
<entry>
<emphasis role="strong">How did you assist
Customer?</emphasis>
</entry>
<entry>
</entry>
</row>
</thead>
<tbody>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
<emphasis role="strong">What difference did this make for the
Customer?</emphasis>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
<row>
<entry>
</entry>
<entry>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
Staff Member: _________________________
</para>
</article>
我已经使用以下 XSLT 成功地精简了它;
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="fileDateStamp">
<xsl:analyze-string select="base-uri(.)" regex="\s*(\d\d\d\d\-\d\d\-\d\d)\s*">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:template match="/">
<impactStatements>
<xsl:apply-templates/>
</impactStatements>
</xsl:template>
<xsl:template match="informaltable/tgroup/thead/row/entry">
<xsl:analyze-string select="normalize-space(.)" regex="\s*How(.*)\s*">
<xsl:matching-substring>
</xsl:matching-substring>
<xsl:non-matching-substring>
<Assisted>
<xsl:value-of select="(.)"/>
</Assisted>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="informaltable/tgroup/tbody/row/entry">
<xsl:analyze-string select="normalize-space(.)" regex="\s*What(.*)\s*">
<xsl:matching-substring>
</xsl:matching-substring>
<xsl:non-matching-substring>
<Difference>
<xsl:value-of select="(.)"/>
</Difference>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="para">
<xsl:analyze-string select="normalize-space(.)" regex="\s*\Staff Member: ([A-Z].*)\s*">
<xsl:matching-substring>
<Staff><xsl:value-of select="regex-group(1)"/></Staff>
<DateCreated><xsl:value-of select="$fileDateStamp"/></DateCreated>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
但我缺少的是能够在每个“记录”周围添加一个标签。因为<informaltable> 和<para> 都是<article> 的孩子,所以我最基本的XSLT 知识完全让我失望。我明白了
<?xml version="1.0" encoding="UTF-8"?>
<impactStatements>
<Assisted>Lorem ipsum dolor sit amet.</Assisted>
<Difference>Lorem ipsum dolor sit amet.</Difference>
<Staff>John Smith</Staff>
<DateCreated>2014-01-01</DateCreated>
<Assisted>Lorem ipsum dolor sit amet.</Assisted>
<Difference>Lorem ipsum dolor sit amet.</Difference>
<Staff>John Smith</Staff>
<DateCreated>2014-01-01</DateCreated>
</impactStatements>
但我想要;
<?xml version="1.0" encoding="UTF-8"?>
<impactStatements>
<statement>
<Assisted>Lorem ipsum dolor sit amet.</Assisted>
<Difference>Lorem ipsum dolor sit amet.</Difference>
<Staff>John Smith</Staff>
<DateCreated>2014-01-01</DateCreated>
</statement>
<statement>
<Assisted>Lorem ipsum dolor sit amet.</Assisted>
<Difference>Lorem ipsum dolor sit amet.</Difference>
<Staff>John Smith</Staff>
<DateCreated>2014-01-01</DateCreated>
</statement>
</impactStatements>
这是一项临时工作,我知道我可以通过其他方式更改 XML,但我确信我只是缺乏一些基本知识来更改 XSLT,我必须做我想做的事。我尝试了各种不同的方法并用谷歌搜索但无济于事。我尝试过的所有操作都会破坏生成的 XML 的格式。
【问题讨论】: