xml - 不使用默认命名空间的没有前缀的 XSL 输出 XML?

标签 xml xslt xml-namespaces

我有一个 XSL,我需要按照以下方式生成输出:

<moo xmlns="http://api.example.com">
    <foo>1358944586848</foo>
    <bar>
        <a>1</a>
        <b>2</b>
        <c>3</c>
    </bar>
</moo>

我可以这样做:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://api.example.com">

    <xsl:template match="/">
        <xsl:element name="moo">
            <!-- and so on -->

但是,我有点讨厌在我的 xsl 文件中使用 xsl 前缀,因为我觉得它很杂乱。无论如何,使用 XPath 进行选择很容易,因为您可以根据需要将 xpath-default-namespace 设置为要转换的任何内容。但是据我所知,没有可用的 element-default-namespace,那么我怎样才能以好的方式生成想要的输出呢?

我知道我可以做到:

<stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/XSL/Transform">

    <template match="/">
        <element name="moo" namespace="http://api.example.com">
            <!-- and so on -->

但是我必须在我创建的每个元素上显式设置此命名空间,否则它们将以 XSL 命名空间结束。那么有没有一种干净的方法来创建具有特定命名空间(没有前缀)并且不触及 xsl 文件的默认命名空间的元素?


更新:

想也许 namespace-alias 可以做点什么,但不知道如何使用它。试过这个,但似乎对输出没有任何影响:

<stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:out="http://api.example.com">

<namespace-alias stylesheet-prefix="out" result-prefix=""/>

    <template match="/">
        <element name="out:moo">
            <!-- and so on -->

namespace-alias 可能不是我想的那样 :p

我使用的最终解决方案,基于 JLRishe 的回答

remove-prefixes.xsl

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <template match="/">
        <variable name="result">
            <next-match />
        </variable>
        <apply-templates select="$result" mode="remove-prefixes" />
    </template>

    <template match="*" priority="1" mode="remove-prefixes">
        <element name="{local-name()}" namespace="{namespace-uri()}">
            <apply-templates select="@* | node()" mode="remove-prefixes" />
        </element>
    </template>
    <template match="@*|node()" mode="remove-prefixes">
        <copy>
            <apply-templates select="@* | node()" mode="remove-prefixes" />
        </copy>
    </template>

</stylesheet>

subject.xsl

<!-- snip -->
<import href="remove-prefixes.xsl" />
<!-- snip -->

最佳答案

您可以做的一件事是将 XSL 的整个结果捕获到一个变量中,然后在末尾清空其前缀:

<stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform"
                          xmlns:p="http://api.example.com">
  <output method="xml" indent="yes"/>

  <template match="/">
    <variable name="result">
      <p:moo>
        <apply-templates />
      </p:moo>
    </variable>
    <apply-templates select="$result" mode="removePrefix" />
  </template>

  <template match="root">
    <p:something hello="hi">
      <element name="p:somethingelse" />
    </p:something>
  </template>

  <template match="p:*" mode="removePrefix">
    <element name="{local-name()}" namespace="{namespace-uri()}">
      <apply-templates select="@* | node()" mode="removePrefix" />
    </element>
  </template>

  <template match="@* | node()" mode="removePrefix">
    <copy>
      <apply-templates select="@* | node()" />
    </copy>
  </template>
</stylesheet>

在此输入上运行时:

<root>
  <top />
</root>

它产生:

<moo xmlns="http://api.example.com">
  <something hello="hi">
    <somethingelse />
  </something>
</moo>

关于xml - 不使用默认命名空间的没有前缀的 XSL 输出 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14480173/

相关文章:

java - 解码为soap webservice 中的通用类型 xml 中的任何类型

xml - 在 XSLT/XML 中将日期显示为 DD-MM-YYYY

c# - 以特定顺序编写 XML 属性和命名空间声明

xml - 使用 `<xs:any>` 对嵌套元素进行不一致的 XSD 验证

jaxb - 使用 binder 解码具有命名空间的 xml 时出现 jaxb moxy 问题

java - 使 ListView 可滚动

C#反序列化XML到对象

python - Flask应用程序错误: failed to load external entity.无法输入xml文件进行解析

javascript - XSLT 和 MathJax

xml - 使用 XSLT 1.0 在 XML 中添加多个命名空间