- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个巨大的 xml 文件,我想将其拆分成单独的 xml 文档;每个单独的 xml 文件都应该有一个明确的命名空间声明,如以下“所需输出”部分所示。但是,我不断收到错误“命名空间错误:未定义关键字上的命名空间前缀 bd”
我的问题是,我如何才能明确地告诉我的 XSLT 处理器将 namespace 声明放在结果输出中的什么位置?我在网上浏览了几个教程,但我不太明白如何解决这个问题。
部分 XSLT 代码段
...
...
<xsl:template match="chapter">
<bd:chapter>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="page" />
</bd:chapter>
</xsl:template>
<xsl:template match="name">
<bd:name>
<xsl:value-of select="." />
</bd:name>
</xsl:template>
...
...
期望的输出
<?xml version="1.0" encoding="utf-8" ?>
<books>
<bd:book xmlns:bd="http://www.bd.org.za/db" xmlns:cd="http://www.bd.org.za/cd">
<bd:name>book01</bd:name>
<bd:chapter>
<cd:name>chapter01<cd:name>
<bd:page>
<cd:title></cd:title>
<pd:description></pd:description>
</bd:page>
</bd:chapter>
</bd:book>
...
...
...
</books>
更新 #1
<?xml version="1.0" encoding="utf-8" ?>
<books>
<book>
<name>book01</name>
<chapter>
<name>chapter01<name>
<page>
<title></title>
<description></description>
</page>
</chapter>
</book>
...
...
...
</books>
更新 #2
@polishchuk Update2 给出以下结果
<?xml version="1.0"?>
<root xmlns:pd="namespace2">
<pd:Book xmlns:pd="http://namespace1.org/">
<pd_1:Name xmlns:pd="namespace2" xmlns:pd_1="http://namespace1.org/">A</pd_1:Name>
<pd:Description xmlns:pd="namespace2">A1</pd:Description>
</pd:Book>
<pd:Book xmlns:pd="http://namespace1.org/">
<pd_1:Name xmlns:pd="namespace2" xmlns:pd_1="http://namespace1.org/">B</pd_1:Name>
<pd:Description xmlns:pd="namespace2">B1</pd:Description>
</pd:Book>
</root>
我希望 namespace 出现的唯一步骤是在书籍节点内。请看下面
<?xml version="1.0"?>
<root>
<pd:Book xmlns:pd="http://namespace1.org/">
<pd:Name >A</pd:Name>
<pd:Description>A1</pd:Description>
</pd:Book>
<pd:Book xmlns:pd="http://namespace1.org/">
<pd:Name>B</pd_1:Name>
<pd:Description>B1</pd:Description>
</pd:Book>
</root>
最佳答案
假设您有以下 XML:
<root>
<book>
<name>A</name>
<description>A1</description>
</book>
<book>
<name>B</name>
<description>B1</description>
</book>
</root>
所需的 XML(带有命名空间):
<root xmlns:bd="namespace1" xmlns:pd="namespace2">
<bd:Book>
<bd:Name>A</bd:Name>
<pd:Description>A1</pd:Description>
</bd:Book>
<bd:Book>
<bd:Name>B</bd:Name>
<pd:Description>B1</pd:Description>
</bd:Book>
</root>
适当的 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bd="namespace1"
xmlns:pd="namespace2"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="book">
<bd:Book>
<xsl:apply-templates />
</bd:Book>
</xsl:template>
<xsl:template match="name">
<bd:Name>
<xsl:value-of select="."/>
</bd:Name>
</xsl:template>
<xsl:template match="description">
<pd:Description>
<xsl:value-of select="."/>
</pd:Description>
</xsl:template>
</xsl:stylesheet>
您只需在 XSLT 中添加命名空间,然后使用命名空间的前缀创建节点。
更新 1:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pd="namespace2"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="book">
<bd:Book xmlns:bd="namespace1">
<xsl:apply-templates />
</bd:Book>
</xsl:template>
<xsl:template match="name">
<bd:Name xmlns:bd="namespace1">
<xsl:value-of select="."/>
</bd:Name>
</xsl:template>
<xsl:template match="description">
<pd:Description>
<xsl:value-of select="."/>
</pd:Description>
</xsl:template>
</xsl:stylesheet>
更新 2:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pd="namespace2"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="book">
<Book xmlns="namespace1">
<xsl:apply-templates />
</Book>
</xsl:template>
<xsl:template match="name">
<Name xmlns="namespace1">
<xsl:value-of select="."/>
</Name>
</xsl:template>
<xsl:template match="description">
<pd:Description>
<xsl:value-of select="."/>
</pd:Description>
</xsl:template>
</xsl:stylesheet>
更新 3:这个 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="book">
<pd:Book xmlns:pd="namespace2">
<xsl:apply-templates />
</pd:Book>
</xsl:template>
<xsl:template match="name">
<pd:Name xmlns:pd="namespace2">
<xsl:value-of select="."/>
</pd:Name>
</xsl:template>
<xsl:template match="description">
<pd:Description xmlns:pd="namespace2">
<xsl:value-of select="."/>
</pd:Description>
</xsl:template>
</xsl:stylesheet>
输出 XML(就像在您的 Update 2 中一样,我使用 MSXML 6.0),但对于 XML 引擎,无论 namespace 在哪里定义:
<root>
<pd:Book xmlns:pd="namespace2">
<pd:Name>A</pd:Name>
<pd:Description>A1</pd:Description>
</pd:Book>
<pd:Book xmlns:pd="namespace2">
<pd:Name>B</pd:Name>
<pd:Description>B1</pd:Description>
</pd:Book>
</root>
关于xml - 命名空间错误 : Namespace prefix bd on keyword is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6797806/
我想知道这在 SQL 中是否可行。假设您有两个表 A 和 B,并且您对表 A 进行选择并在表 B 上进行联接: SELECT a.*, b.* FROM TABLE_A a JOIN TABLE_B
我认为这只是一个一般的 C++ 问题: 我正在尝试使用 gnu c++ 编译器在 Linux Fedora 上编译本地版本的 ffmpeg。我的源代码位于以下一堆文件夹中: ~//Downloads/
我听说在 Linux 上编译 PHP 时设置 --prefix=PREFIX 选项将允许您一次安装多个 PHP 而不会发生冲突。 (我认为如果未设置默认值是 /usr/local)。但是,我不确定它到
我有以下 conda 环境文件 environment.yml: name: testproject channels: - defaults dependencies: - python=3.7 p
目前在我的 Makefile 中我有: prefix ?= /usr/local 这样我就可以在调用 make 时覆盖 prefix 值,如下所示: make prefix="/new_path" 我
我正在尝试创建一个计算 pptx 中单词的函数文档。问题是我不知道如何只找到这种标签: Some Text 当我尝试:print xmlTree.findall('.//a:t') , 它返回 Syn
gitconfig(1) : If not set explicitly with --file, there are four files where git config will search
嗨,我是 Spring MVC 的新手,我正在关注 Spring 引用文档,我对 View 解析器有疑问。这是我的示例代码。 @Controller @RequestMapping("/form")
我一般都是通过pip安装python包的。 对于 Google App Engine,我需要将包安装到另一个目标目录。 我试过了: pip install -I flask-restful --tar
我对一些使用指令 ngb-pagination 的组件进行了 Angular 测试。来自 ng-bootstrap . 现在,在我的测试中,我模拟这个组件如下: // on next line I g
我无法运行npm install npm@latest -g,而不会遇到权限错误并被迫使用sudo npm install npm@latest -g。我可以在不使用 sudo 的情况下运行它,但是
假设我有一个字符串公式,它的格式是:“func(a+b,c)”,其中func是一个定制函数,这个字符串同时包含中缀(即+)和前缀(即func)表示,我想将其转换为具有所有前缀表示的字符串,“func(
我已经写下了一个简单的函数来确定 str1 是否是 str2 的前缀。这是一个非常简单的函数,看起来像这样(在 JS 中): function isPrefix(str1, str2) // dete
我们有 git remote add origin http://...避免重复输入实际的源代码库路径。但是如何git subtree --prefix=... ?每次拉/推子树内容时,很难跟踪、记住
我的 travis 工作遇到了以下问题: Process Output:Could not find platform independent libraries Process Output:Co
我正在阅读 Artifice 的来源并看到: module Artifice NET_HTTP = ::Net::HTTP # ... end 行:https://github.com/wyc
我正在阅读有关 Angular 路由的文档并创建了一个简单的测试: const routes: Route[] = [ { path: '', redirectTo: '/home', pat
所以使用 Material 我们有一些代码来问一个问题,我们想在输入的前面添加一个 $ 符号或使用占位符。在这个垫子字段内部,我们还有一个垫子标签。如果我使用 matprefix,它会将美元符号放在标
目前我希望为 Apache Mesos 编写一个脚本来启动主/从(在 2 个不同的节点上)。 我有引用http://mesos.apache.org/documentation/latest/depl
我已为文档建立索引,每个文档都有一个字段:“CodeName”,其值类似于以下内容: document 1 has CodeName: "AAA01" document 2 has CodeName:
我是一名优秀的程序员,十分优秀!