【问题标题】:Reference to undeclared namespace prefix: 'json'. while renaming xml attibute with xslt引用未声明的命名空间前缀:'json'。使用 xslt 重命名 xml 属性时
【发布时间】:2017-04-09 03:05:47
【问题描述】:

更新了最少、完整和可验证的信息,感谢 @Kenneth 引起我的注意。

所以,我正在尝试使用 xslt 重命名 xml 属性,但我不断收到以下错误消息:

引用未声明的命名空间前缀:'json'。

  1. 这是我的源 xml:

    <?xml version="1.0" encoding="utf-8"?>
    <data>
      <code>SO000009</code>
      <businessPartner>70833A356B9A428CBDDCD2A76A49681F</businessPartner>
      <startDateTime>2018-01-25T15:24:27Z</startDateTime>
      <subject>Test</subject>
      <equipments jsonArray="true">80202</equipments>
    </data>
    
  2. 这就是我希望 xml 的样子:

    <?xml version="1.0" encoding="utf-16"?>
    <data xmlns:json="http://james.newtonking.com/projects/json">
      <code>SO000009</code>
      <businessPartner>70833A356B9A428CBDDCD2A76A49681F</businessPartner>
      <startDateTime>2018-01-25T15:24:27Z</startDateTime>
      <subject>Test</subject>
      <equipments json:Array="true">80202</equipments>
    </data>
    
  3. 这是我的 xslt:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:template match="/data">
        <data xmlns:json="http://james.newtonking.com/projects/json">
          <xsl:apply-templates select="node()|@*" />
        </data>
      </xsl:template>
    
      <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="node()">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="@jsonArray">
        <xsl:attribute name="json:Array">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:template>
    </xsl:stylesheet>
    

当我尝试转换我的 xml 时,我不断收到此消息:

编译样式表“CS_jsonArray.xslt”时出错。

代码:0x80004005

引用未声明的命名空间前缀:'json'。

【问题讨论】:

  • 切勿将代码和 XML 作为图像发布。使用格式化为代码的文本。单击edit 链接并更正。请务必包含minimal reproducible example
  • 当我尝试转换我的 xml 时...这个试用版在哪里?
  • 解决方法是声明未声明的命名空间前缀:'json'。您没有向我们展示您的样式表,但如果您搜索它们,您可以找到很多示例。

标签: json xml xslt


【解决方案1】:

查看“相关”侧边栏后,我确实找到了自己问题的答案。

XSL transformation - Namespace prefix undeclared

所以对于那些有同样问题的人,请确保要更改属性名称的命名空间也在 xslt 标记本身中......

我的 xslt 就是这样开始的:

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

这就是我改变它的方式:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:json="http://james.newtonking.com/projects/json">
  <xsl:output indent="yes"/>...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多