xslt 1.0 - 生成默认命名空间约定的转换 xmlns= "..."



给定以下xml:

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

xslt (版本 1.0) 可用于生成:

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

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:e="example.com" xmlns:s="stackoverflow.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>

我认为排除结果前缀和命名空间别名是票证,因为我可以通过在我的样式表中声明它们来显式使用每个命名空间,然后在将/projection 命名空间别名化为 #default 时删除/public 命名空间。但是,别名会导致简单地删除命名空间。

很难理解你的问题中什么是真实的,什么是一个例子。从字面上理解你的例子(就像你自己的答案一样),答案是:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="stackoverflow.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>

一个更通用的答案,将所有元素从其现有命名空间移动到新命名空间是:

<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="stackoverflow.com">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

如果您只想从字面上匹配输入根目录并逐字输出结果文档,只需这样做:

<?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="stackoverflow.com">
      <child/>
    </newRoot>
  </xsl:template>
</xsl:stylesheet>

这有效,但可能满足您的一般需求,也可能不满足您的一般需求。 下面是您或其他人可能会发现更有用的另一个转换......

XSLT 更改所有元素命名空间

以下 XSLT 会将所有元素的命名空间更改为 stackoverflow.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="stackoverflow.com">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

最新更新