生成默认名称空间约定的转换 xmlns="..."

Transform which produces default namespace convention xmlns="..."

给出以下 xml:

<root xmlns="example.com">
  <child />
</root>

什么xslt(1.0版)可以用来制作:

<newRoot xmlns="whosebug.com">
  <child />
</newroot>

我尝试了排除结果前缀和名称空间别名的各种组合。例如,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:e="example.com" xmlns:s="whosebug.com" exclude-result-prefixes="e">

<xsl:namespace-alias stylesheet-prefix="s" result-prefix="#default" />

    <xsl:template match="e:root">
        <s:newRoot>
            <xsl:apply-templates />
        </s:newRoot>
    </xsl:template>

    <xsl:template match="e:child">
        <s:child />
    </xsl:template>

</xsl:stylesheet>

我最接近的是以下不正确的xml

<newRoot>
  <child />
</newroot>

编辑:对于过于简单的示例,我深表歉意。 我正在搜索的解决方案将更改默认名称空间并对我的 xml 进行转换。这是我想做的更复杂的例子:

<Data xmlns="sample.com/public">
  <Insured>
    <Name>Sample Client</Name>
    <Locations>
      <Location Number="1">
        <Address>
          <City>New York</City>
          <State>New York</State>
          <Street>222 10 Street</Street>
        </Address>
      </Location>
      <Location Number="2">
        <Address>
          <City>New York</City>
          <State>New York</State>
          <Street>3538 27 Street</Street>
        </Address>
      </Location>
    </Locations>
    <Coverages>
      <LocationSplit>
        <LocationNumber>1</LocationNumber>
        <Clause>
          <Limit>10000000</Limit>
        </Clause>
      </LocationSplit>
    </Coverages>
  </Insured>
</Data>

这会变成

<projection xmlns="sample.com/projected">
  <insured>
    <name>Sample Client</name>
    <location address="222 10 Street New York, New York">
      <clause>
        <limit>10000000</limit>
      </clause>
    </location>
  </insured>
</projection>

我认为 exclude-result-prefix 和 namespace-alias 是票,因为我可以通过在我的样式表中声明它们来显式使用每个命名空间,然后删除 /public 命名空间,同时将 /projection 命名空间别名为#默认值。但是,别名导致名称空间被简单地删除。

如果您只想按字面匹配输入根并按字面输出结果文档,只需这样做:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:ex="example.com"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="ex">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/ex:root">
    <newRoot xmlns="whosebug.com">
      <child/>
    </newRoot>
  </xsl:template>

</xsl:stylesheet>

可行,但不一定能满足您的一般需求。以下是您或其他人可能会觉得更有用的另一种转换...

用于更改所有元素命名空间的 XSLT

以下 XSLT 会将所有元素的命名空间更改为 whosebug.com:

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

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

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

</xsl:stylesheet>

很难理解您问题中的真实情况和示例。从字面上看你的例子(就像你自己的答案一样),答案是:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="whosebug.com"
xmlns:e="example.com" 
exclude-result-prefixes="e">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="e:child">
    <child/>
</xsl:template>

</xsl:stylesheet>

all 元素从其现有 namespace/s 移动到新命名空间的更通用的答案是:

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

<xsl:template match="*">
    <xsl:element name="{local-name()}" namespace="whosebug.com">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>