【问题标题】:I can try use xmllint but not found我可以尝试使用 xmllint 但未找到
【发布时间】:2017-10-27 10:32:04
【问题描述】:

我可以尝试使用 xmllint 在 SQL 上转换 XML。

我有这个 XML,我需要在 SQL 中进行转换。

 <CommunicationLog xmlns="http://knx.org/xml/telegrams/01">
  <RecordStart Timestamp="" Mode="" Host="" ConnectionName="" ConnectionOptions="" ConnectorType="" MediumType="" />
  <Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
  <Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
  <RecordStart Timestamp="" Mode="" Host="" ConnectionName="" ConnectionOptions="" ConnectorType="" MediumType="" />
  <Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
  <Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
  <RecordStop Timestamp="" />
</CommunicationLog>

我在 bash 中有这个脚本,带有用于转换的 xmllint

#!/bin/bash

file="$1"
table=$(xmllint --xpath "name()" "$file")
value=$(xmllint --xpath "//*/*/text()" "$file")

cat << EOF
INSERT INTO \`${table}\`
VALUES
(${value})
EOF

我的输出:

XPath set is empty

我可以尝试使用 XML_LOAD();在 sql server 但没有找到我。数据已被删除,因为它是私有的。

【问题讨论】:

    标签: sql-server xml xpath xquery


    【解决方案1】:

    你对这个XPath有什么期待?

    //*/*/text()
    

    这将对任何节点进行深度搜索,然后深入一个节点并获取text()。

    您的 XML 以属性为中心...并且有一个默认命名空间 (xmlns)...

    我不知道你想要达到什么目标。只是为了测试你可能会尝试这样的事情

    /*:CommunicationLog/*:*/@*:Timestamp     
    

    *: 是命名空间的通配符(但最好正确声明它!)。它将从&lt;CommunicationLog&gt; 开始并遍历下面的所有节点。它们似乎都具有时间戳属性。这应该返回一些东西......

    如果您找不到出路,请在您的 XML 中插入一些虚拟数据并说明预期的输出。

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多