html - 想要在没有默认命名空间的情况下复制子元素

标签 html xml

我正在做 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>

但是,在这里,我的问题是,我没有从模板 <xsl:template match= 中获取任何子元素,例如 ema “o/内容”。我在这上面犯了什么错误。请解释。提前致谢。

最佳答案

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

<xsl:template match="o/content">
  <section class="content">
    <xsl:copy-of select="node()"/>
  </section>
</xsl:template>

关于html - 想要在没有默认命名空间的情况下复制子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102840/

相关文章:

java - 根据 Activity 配置文件限制 spring 资源

c# - 如何根据自定义顺序订购列表项?

python - 使用 Python 从 ElementTree 中删除 XML 版本标签

c# - 序列化字典包装器时出现问题

javascript - 使用 JS 在特定的流体页面中隐藏页脚

HTML 表格,colspan 问题

html - 如何修复/适合这个 div 容器?

javascript - 是否有一个 xmpp 客户端可以在页面刷新时保持聊天?

java - 将 JSON 对象动态转换为表行

java - 我需要读取一个xml文件并将每个标签的id更改为id+tagname