gpt4 book ai didi

xml - 命名空间错误 : Namespace prefix bd on keyword is not defined

转载 作者:数据小太阳 更新时间:2023-10-29 02:37:26 26 4
gpt4 key购买 nike

我有一个巨大的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com