最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xml - How can I omit a namespace in xslt output? - Stack Overflow

programmeradmin4浏览0评论

I have this XML

<?xml version="1.0" encoding="UTF-8"?>
<item xmlns="; version="2.2" xml:lang="en-us">
    <head>
        <title>Scope of application</title>
        <id>a71ad0e3e1ed569595c88b60421846ee</id>
        <version>2</version>
    </head>
    <body lang="en">
      <section id="d29e3458" data-class="portrait smo-chapter-orientation-portrait smo-chapter-numbering-no numbering-no lvl- 1" data-type="Chapter">
         <p class="smo-hide" data-role="heading" id="d29e3461">System requirements</p>
         <div data-role="block" id="d29e3525"/>
      </section>
   </body>
</item>

Which I am transforming to this html via XSLT 2.0

Output:

<!DOCTYPE HTML>
<html xmlns=";>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title>Scope of application</title>
      <meta name="id" content="32a80e1803f0966289c5c7d3075abcd8"/>
   </head>
</html>

This is my stylesheet:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
                xmlns:xsl=";
                xml:lang="en"
                xmlns=";
                xpath-default-namespace=";
                exclude-result-prefixes=""
>

    <xsl:output method="html" encoding="utf-8" indent="yes"/>

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

<!-- other template rules, omitted for readability  -->

</xsl:stylesheet>   

I want to get rid of the xmlns=" in the output, but I don't know how to achieve this. exclude-result-prefixes="" doesn't work.

Could someone please guide me? Thank you.

I have this XML

<?xml version="1.0" encoding="UTF-8"?>
<item xmlns="http://www.p-corp/ns/pyxml" version="2.2" xml:lang="en-us">
    <head>
        <title>Scope of application</title>
        <id>a71ad0e3e1ed569595c88b60421846ee</id>
        <version>2</version>
    </head>
    <body lang="en">
      <section id="d29e3458" data-class="portrait smo-chapter-orientation-portrait smo-chapter-numbering-no numbering-no lvl- 1" data-type="Chapter">
         <p class="smo-hide" data-role="heading" id="d29e3461">System requirements</p>
         <div data-role="block" id="d29e3525"/>
      </section>
   </body>
</item>

Which I am transforming to this html via XSLT 2.0

Output:

<!DOCTYPE HTML>
<html xmlns="http://www.p-corp/ns/pyxml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title>Scope of application</title>
      <meta name="id" content="32a80e1803f0966289c5c7d3075abcd8"/>
   </head>
</html>

This is my stylesheet:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3./1999/XSL/Transform"
                xml:lang="en"
                xmlns="http://www.p-corp/ns/pyxml"
                xpath-default-namespace="http://www.p-corp/ns/pyxml"
                exclude-result-prefixes=""
>

    <xsl:output method="html" encoding="utf-8" indent="yes"/>

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

<!-- other template rules, omitted for readability  -->

</xsl:stylesheet>   

I want to get rid of the xmlns="http://www.p-corp/ns/pyxml in the output, but I don't know how to achieve this. exclude-result-prefixes="" doesn't work.

Could someone please guide me? Thank you.

Share Improve this question edited Mar 21 at 18:13 ondrums asked Mar 20 at 7:34 ondrumsondrums 17812 bronze badges 9
  • Before considering the question of removing the namespace declaration (which is actually a question of renaming all elements in that namespace): your stylesheet does NOT produce the result you claim, not even close to it. – michael.hor257k Commented Mar 20 at 8:28
  • For the renaming part, see (for example): stackoverflow/a/71846808/3016153 – michael.hor257k Commented Mar 20 at 9:31
  • If you don't want any namespace for the HTML result elements then why does your XSLT declare xmlns="http://www.p-corp/ns/pyxml"? That will certainly put that literal result <html> element that you have in that namespace. – Martin Honnen Commented Mar 20 at 11:35
  • 2 This question is similar to: Remove Namespace and add new elements while copying XML. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Joe Commented Mar 20 at 12:04
  • 2 The key to this is: don't worry about the namespace declarations, worry about the element names. If you get the element names right (and remember, an element name has two parts, the namespace and the local part), then the namespace declarations will look after themselves. You have namespace declaration you don't want because you have constructed an element in the wrong namespace. – Michael Kay Commented Mar 20 at 12:39
 |  Show 4 more comments

1 Answer 1

Reset to default 1

Using an identity transform, with a specialized template for elements that constructs an element using the local-name() (which will produce an element without that namespace) rather than xsl:copy (which would preserve the namespace) from the default template match.

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3./1999/XSL/Transform"
  xml:lang="en">

  <xsl:output method="html" encoding="utf-8" indent="yes"/>

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

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

</xsl:stylesheet> 
发布评论

评论列表(0)

  1. 暂无评论