xml自动输出时存在空格问题的字段名称

发布于 2024-10-13 03:57:14 字数 929 浏览 1 评论 0原文

我的sql非常简单,

当我执行此sql时,选择ID为[员工ID],EmpName为[员工姓名],Sal为[薪水] from Emp FOR XML AUTO, ELEMENTS, ROOT('customers')

然后我得到输出xml 格式。 xml 输出是

<customers>
<Emp>
  <Employee_x0020_ID>1</Employee_x0020_ID>
  <Employee_x0020_Name>Tridip</Employee_x0020_Name>
  <Salary>2500</Salary>
</Emp>
<Emp>
  <Employee_x0020_ID>2</Employee_x0020_ID>
  <Employee_x0020_Name>Ari</Employee_x0020_Name>
  <Salary>4000</Salary>
</Emp>
<Emp>
  <Employee_x0020_ID>3</Employee_x0020_ID>
  <Employee_x0020_Name>Dibyendu</Employee_x0020_Name>
  <Salary>3500</Salary>
</Emp>
</customers>

如果您看到 xml 字段名称那么您可以理解字段名称是动态生成的 由于空间。1这是动态生成的,但我希望它应该像1那样生成。我希望在 xml 文件名中保留空间。所以请告诉我该怎么做......谢谢

my sql is very simple

select ID as [Employee ID], EmpName as [Employee Name], Sal as [Salary] from Emp FOR XML AUTO, ELEMENTS, ROOT('customers')

when i execute this sql then i am getting output in xml format. the xml output is

<customers>
<Emp>
  <Employee_x0020_ID>1</Employee_x0020_ID>
  <Employee_x0020_Name>Tridip</Employee_x0020_Name>
  <Salary>2500</Salary>
</Emp>
<Emp>
  <Employee_x0020_ID>2</Employee_x0020_ID>
  <Employee_x0020_Name>Ari</Employee_x0020_Name>
  <Salary>4000</Salary>
</Emp>
<Emp>
  <Employee_x0020_ID>3</Employee_x0020_ID>
  <Employee_x0020_Name>Dibyendu</Employee_x0020_Name>
  <Salary>3500</Salary>
</Emp>
</customers>

if u see the xml filed name then u can understand that field name is generated dynamically
due to space. <Employee_x0020_ID>1</Employee_x0020_ID> this is dynamically generated but i want it should be generated like <Employee ID>1</Employee ID>. i want space should be maintain in the xml filed name. so please tell me what to do........thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-10-20 03:57:14

来自 W3Schools 的 XML 元素

XML 元素必须遵循以下命名规则:

  • 名称可以包含字母、数字和其他字符
  • 名称不能以数字或标点符号开头
  • 名称不能以字母 xml(或 XML、Xml 等)
  • 开头 名称不能包含空格

可以使用任何名称,不保留任何单词。

(强调已添加)

From XML Elements at W3Schools:

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names cannot start with a number or punctuation character
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces

Any name can be used, no words are reserved.

(Emphasis added)

旧夏天 2024-10-20 03:57:14

我想这是同一个问题?
FOR XML AUTO 和列名称对于SQL Server 2005中的空间问题

答案是一样的。 XML 标记名称中不能有空格。

I guess this is the same question?
FOR XML AUTO and column name with space problem in SQL Server 2005

The answer is the same. You can not have a space in a XML tag name.

顾忌 2024-10-20 03:57:14

以下是您正在寻找的假装为 XML 的输出。
您甚至无法在任何解析器中将其转换为 XML(至少不应该——在 SQL Server 中不会)。

;with emp(ID, EmpName, Sal) as (select
    1, 'tridip', 2500 union all select
    2, 'ari', 4000)

select replace(convert(varchar(max),(
    select ID as [Employee ID], EmpName as [Employee Name], Sal as [Salary] 
    from Emp 
    FOR XML AUTO, ELEMENTS, ROOT('customers'))),
    '_x0020_', ' ')

输出(为了清晰起见,重新格式化 - SQL Server 在一行上返回所有内容)

<customers><Emp><Employee ID>1</Employee ID><Employee Name>tridip
</Employee Name><Salary>2500</Salary></Emp><Emp>
<Employee ID>2</Employee ID><Employee Name>ari</Employee Name>
<Salary>4000</Salary></Emp></customers>

Here is the output you are looking for that pretends to be XML.
You won't even be able to cast it to XML in any parser (at least it shouldn't - it won't in SQL Server).

;with emp(ID, EmpName, Sal) as (select
    1, 'tridip', 2500 union all select
    2, 'ari', 4000)

select replace(convert(varchar(max),(
    select ID as [Employee ID], EmpName as [Employee Name], Sal as [Salary] 
    from Emp 
    FOR XML AUTO, ELEMENTS, ROOT('customers'))),
    '_x0020_', ' ')

Output (reformatted for clarity - SQL Server returns it all on one line)

<customers><Emp><Employee ID>1</Employee ID><Employee Name>tridip
</Employee Name><Salary>2500</Salary></Emp><Emp>
<Employee ID>2</Employee ID><Employee Name>ari</Employee Name>
<Salary>4000</Salary></Emp></customers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文