2

我正在做 XML 到 HTML 的转换,我制定了 xslt,在这个问题上,仍然存在一个我无法解决的问题。

<?xml version="1.0" encoding="UTF-8"?>
<o>
<abstract-short type="string"><p>Last month a high court judge ruled that, for the purposes of inheritance tax, all let property is classed the same&#x2026;</p></abstract-short>
<content type="string"><p>Last month a<em>high court</em> judgr may<a href="htpp://www.ibm.com">deal</a> with weekly (or r queries and requests then it is still classed an investment, not a business.</p><p>An appeal against this<em>decision</em> destourist industry and the role it plays in the wider South West economy.</p></content>
<created type="string">2013-02-21T23:59:00</created>
<creator class="object">
<text type="string">[creatorName]</text>
<uri type="string">[creator]</uri>
</creator>
<identifier type="string">https://www.gazettes.co.uk/content/6</identifier>
<issued type="string">2013-06-31T23:59:00</issued>
<position type="string">related pane first</position>
<relation class="array">
<e type="string">wills-and-probate</e>
</relation>
<rights type="string">[copyrightattributionURI]</rights>
<source class="object">
<text type="string">Western Morning News</text>
<uri type="string">http://www.thisisdevon.co.uk/story-18208063-detail/story.html#axzz2M0VVYB82</uri>
</source>
<subject type="string">news</subject>
<title type="string">Arguing the case for appeal over tax ruling</title>
<weight type="string">0</weight>
</o>

我的 xslt 代码是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="#all">

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>


<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="o">
<xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html></xsl:text>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  prefix="dcterms: http://purl.org/dc/terms/ dc: http://purl.org/dc/elements/1.1/ gz: https://www.gazettes.co.uk/metadata">
<xsl:apply-templates select="title"/>
</html>
</xsl:template>


<xsl:template match="o/title">
<head>
<title property="dc:title">
<xsl:attribute name="about">
<xsl:value-of select="./parent::o/child::identifier"/>
</xsl:attribute>
<xsl:apply-templates/>
</title>
<meta name="dcterms.format" content="application/xhtml+xml"/>
<xsl:apply-templates
  select="./parent::o/child::subject, ./parent::o/child::identifier, ./parent::o/child::relation, ./parent::o/child::position, ./parent::o/child::weight"
  />
</head>
<xsl:apply-templates select="./parent::o/child::created[@type='string']"/>
</xsl:template>

<!-- # Matching Meta content -->

<xsl:template match="o/created[@type='string']">
<body>
<article>
<header>
<h1 class="title">
<xsl:value-of select="./parent::o/child::title[@type='string']"/>
</h1>
</header>
<dl>
<dt>Created date</dt>
<dd property="dcterms:created">
<xsl:attribute name="content">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:attribute name="about">
<xsl:value-of select="./parent::o/child::identifier"/>
</xsl:attribute>
<xsl:value-of select="substring-before(., 'T')"/>
</dd>
<xsl:apply-templates select="./parent::o/child::issued, ./parent::o/child::source"/>
</dl>
<xsl:apply-templates
select="./parent::o/child::abstract-short, ./parent::o/child::abstract-long, ./parent::o/child::content"
/>
</article>
</body>
</xsl:template>


<xsl:template match="o/subject">
<meta name="dcterms.subject">
<xsl:attribute name="content">
<xsl:apply-templates/>
</xsl:attribute>
</meta>
</xsl:template>

<xsl:template match="o/identifier">
<meta name="dcterms.identifier">
<xsl:attribute name="content">
<xsl:apply-templates/>
</xsl:attribute>
</meta>
</xsl:template>

<xsl:template match="o/relation/e">
<meta name="dcterms.relation">
<xsl:attribute name="content">
<xsl:apply-templates/>
</xsl:attribute>
<xsl:attribute name="scheme">
<xsl:value-of select="'isPartOf'"/>
</xsl:attribute>
</meta>
</xsl:template>

<xsl:template match="o/position">
<meta name="gz.position">
<xsl:attribute name="content">
<xsl:apply-templates/>
</xsl:attribute>
</meta>
</xsl:template>

<xsl:template match="o/weight">
<meta name="gz.weight">
<xsl:attribute name="content">
<xsl:apply-templates/>
</xsl:attribute>
</meta>
</xsl:template>


<xsl:template match="o/issued">
<dt>Publication date</dt>
<dd property="dcterms:issued">
<xsl:attribute name="content">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:attribute name="about">
<xsl:value-of select="./parent::o/child::identifier"/>
</xsl:attribute>
<xsl:value-of select="substring-before(., 'T')"/>
</dd>
</xsl:template>

<xsl:template match="o/source">
<dt>Source</dt>
<dd property="dc:source">
<xsl:attribute name="content">
<xsl:value-of select="./uri[@type='string']"/>
</xsl:attribute>
<xsl:attribute name="about">
<xsl:value-of select="./parent::o/child::identifier"/>
</xsl:attribute>
<xsl:value-of select="./text[@type='string']"/>
</dd>
</xsl:template>

<!-- # Matching abstract short and long content -->

<xsl:template match="o/abstract-long">
<section class="abstract-long" property="dcterms:abstract">
<xsl:attribute name="about">
<xsl:value-of select="./parent::o/child::identifier"/>
</xsl:attribute>
<xsl:for-each select="./*">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:for-each>
</section>
</xsl:template>

<xsl:template match="o/abstract-short">
<section class="abstract-short" property="dcterms:abstract">
<xsl:attribute name="about">
<xsl:value-of select="./parent::o/child::identifier"/>
</xsl:attribute>
<xsl:for-each select="./*">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:for-each>
</section>
</xsl:template>

<!-- # Matching body content -->

<xsl:template match="o/content">
<section class="content">
<xsl:for-each select="./*">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:for-each>
</section>
</xsl:template>
<xsl:template match="o/rights"></xsl:template>
<xsl:template match="o/creator"></xsl:template>
</xsl:stylesheet>

但是,在这里,我的问题是,我没有从模板中获得任何子元素,例如em, 。我在这方面犯了什么错误。请解释一下。在此先感谢。a&lt;xsl:template match="o/content"

4

1 回答 1

0

如果您只想将<content>...</content>更改的所有子项复制<xsl:template match="o/content">到:

<xsl:template match="o/content">
  <section class="content">
    <xsl:copy-of select="node()"/>
  </section>
</xsl:template>
于 2014-07-29T12:24:16.763 回答