1

您好我想xmlns="http://www.w3.org/1999/xhtml"从源 xhtml/xml 文件中删除命名空间并重命名根节点,并且想尝试仅使用一个样式表来执行此操作。这可能吗?

这是我的示例源 xml/xhtml:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title/>
  <meta/>
</head>

<body link="#000000" bgcolor="#FFFFFF" leftmargin="0">
<div>
  <p>Here is a paragraph</p>
</div>
</body>
</html>

这是我想要的输出:

<document>
  <section>
    <paragraph>Here is a paragraph</paragraph>
  </section>
</document>

目前,我只能使用两个样式表来实现这个结果(见下文),但我希望能够将这些指令组合到一个样式表中。这可能吗?如果可以,怎么做?提前感谢您的帮助。

当前样式表 1:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"/>

<xsl:template match="*|@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="*|@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
<xsl:apply-templates/>`
</xsl:template>`

<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="*|@*|node()"/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

当前样式表 2:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="*|@*|text()">
  <xsl:copy>
    <xsl:apply-templates select="*|@*|text()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="html">
  <document>
    <xsl:apply-templates select="*|@*|text()"/>
  </document>
</xsl:template>

<xsl:template match="div">
  <section>
    <xsl:apply-templates select="*|@*|text()"/>
  </section>`
</xsl:template>`

<xsl:template match="p">
  <paragraph>
    <xsl:apply-templates select="*|@*|text()"/>
  </paragraph>
</xsl:template>`

<xsl:template match="head"/>

</xsl:stylesheet>
4

4 回答 4

3

由于您使用的是 XSLT 2.0,因此您需要做的就是添加

xpath-default-namespace="http://www.w3.org/1999/xhtml"

到样式表 2,工作就完成了。

于 2012-11-13T00:26:28.937 回答
1

对于 xslt 2.0 解决方案,添加到 Michael Kay 的较早答案,以下模板还将停止将命名空间添加到根元素的子元素:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xpath-default-namespace="http://www.w3.org/1999/xhtml">

<xsl:template match="*[namespace-uri() = 'http://www.w3.org/1999/xhtml']">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="*|@*|text()"/>
</xsl:element>
</xsl:template> 

</xsl:stylesheet>
于 2014-08-25T18:06:14.777 回答
1

只是这个

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

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

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

 <xsl:template match="x:p">
  <paragraph><xsl:apply-templates/></paragraph>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title/>
        <meta/>
    </head>
    <body link="#000000" bgcolor="#FFFFFF" leftmargin="0">
        <div>
            <p>Here is a paragraph</p>
        </div>
    </body>
</html>

产生了想要的正确结果

<document>
   <section>
      <paragraph>Here is a paragraph</paragraph>
   </section>
</document>
于 2012-11-13T03:56:52.997 回答
1

有几种不同的方法可以做到这一点。这是一个。

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="xsl xhtml">
<xsl:output encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*|node()" />
  </xsl:element>
</xsl:template>

<xsl:template match="@*|comment()|processing-instruction()|text()">
  <xsl:copy />
</xsl:template>

<xsl:template match="/*"><!-- xhtml:html -->
  <document>
    <xsl:apply-templates select="@*|node()" />
  </document>
</xsl:template>

<xsl:template match="xhtml:body">
  <xsl:apply-templates select="@*|node()" />
</xsl:template>

<xsl:template match="xhtml:div">
  <section>
    <xsl:apply-templates select="@*|node()" />
  </section>`
</xsl:template>`

<xsl:template match="xhtml:p">
  <paragraph>
    <xsl:apply-templates select="@*|node()" />
  </paragraph>
</xsl:template>`

<xsl:template match="xhtml:head"/>

</xsl:stylesheet>

更新 我忘了将 xhtml: 添加到模板匹配中。现已更正。

于 2012-11-12T23:11:33.317 回答