xml - Parse XML with SQL Server 2016
问题描述
In the past when I had
<Article PubModel="Print-Electronic">
<Journal>
<ISSN IssnType="Electronic">1360-0443</ISSN>
<JournalIssue CitedMedium="Internet">
or
<Journal>
<ISSN IssnType="Print">2234-943X</ISSN>
<JournalIssue CitedMedium="Print">
<Volume>8</Volume>
I would pull the ISSN number in my stored procedure using
nref.value('Article[1]/Journal[1]/ISSN[1]','varchar(max)') ISSN,
Now I want to identify which type of ISSN it is but I'm unsure where to put the @Print or @Electronic.
I've tried
nref.value('Article[1]/Journal[1]/ISSN[1]/@Electronic', 'varchar(max)') ISSN,
and
nref.value('Article[1]/Journal[1]/@Electronic', 'varchar(max)') ISSN,
Neither worked. I want to put the Print ISSN into the ISSN_Print field if there is one and the Electronic in the ISSN_Electronic field if there is one.
Thanks for the help
解决方案
I fudged your XML a bit to get a working example. But this should get you what you're looking for:
declare @x xml = '<Journal>
<ISSN IssnType="Print">2234-943X</ISSN>
<ISSN IssnType="Electronic">1360-0443</ISSN>
<JournalIssue CitedMedium="Print"/>
<Volume>8</Volume></Journal>';
select @x.value('(Journal/ISSN[@IssnType="Print"])[1]', 'varchar(40)'),
@x.value('(Journal/ISSN[@IssnType="Electronic"])[1]', 'varchar(40)');
Specifically, instead of using an ordinal selector (like [1]
), you'll be using a property selector instead.
推荐阅读
- android - 如何在 android retrofit 2 中上传照片?
- python - 如何阻止计数器增加
- sql - 如何删除重复记录(这是一个棘手的情况,行本质上不是唯一的,但它们的内容是)
- objective-c - 如何根据我的 API 的响应在 UITableViewCell 中使用 AVPlayer 和 Image
- python - 使用 QMediaPlayer.duration() 时总是得到 0
- java - 获取列表中特定字符串的最后一次出现
- reactjs - 滚动时可折叠的顶部选项卡导航器
- mongodb - 保存后使用猫鼬精益
- google-sheets - 如何在 Google 表格中找到现有图表的图表 ID?
- android - 将 JSON 反序列化为包含对另一个反序列化对象的引用的类型