在下面的 XML 中,當任何 /Bundle/entry/resource/Patient/contained/Patient/identifier/system 值屬性包含“remove-this -Patient" ,我可以使用完整值 == "https://example.com/remove-this-Patient" 但“包含”對我來說更好,因為 url 部分可以來自多個地方并且略有不同。
我已經嘗試了下面的兩個代碼示例和其他變體,但都不起作用。代碼運行沒有錯誤,但沒有洗掉目標患者元素。
就像測驗一樣,我嘗試在“where”子句中使用 /Bundle/entry/resource/Patient/contained/Patient/id 元素并且我能夠讓它作業,所以我認為它與 / 有關Bundle/entry/resource/Patient/contained/Patient/identifier 元素在 Patient 元素內重復。
啟動 XML
<Bundle>
<id value="xxxx" />
<entry>
<fullUrl value="xxxxxxx" />
<resource>
<Patient>
<id value="xxxx" />
<contained>
<Practitioner>
<id value="xx"/>
</Practitioner>
</contained>
<contained>
<Patient>
<id value="xxxx" />
<identifier>
<type>
<coding>
</coding>
</type>
<system value="http://example.com/remove-this-Patient" />
<value value="xxx" />
</identifier>
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-value" />
<value value="xxx" />
</identifier>
</Patient>
</contained>
<contained>
<Patient>
<id value="xxxx" />
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-thing" />
<value value="xxx" />
</identifier>
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-value" />
<value value="xxx" />
</identifier>
</Patient>
</contained>
</Patient>
</resource>
</entry>
</Bundle>
當子元素識別符號/系統值 = "http://example.com/remove-this-Patient" 時,所需的輸出將洗掉 /contained/Patient 元素
<Bundle>
<id value="xxxx" />
<entry>
<fullUrl value="xxxxxxx" />
<resource>
<Patient>
<id value="xxxx" />
<contained>
<Practitioner>
<id value="xx"/>
</Practitioner>
</contained>
<contained>
</contained>
<contained>
<Patient>
<id value="xxxx" />
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-thing" />
<value value="xxx" />
</identifier>
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-value" />
<value value="xxx" />
</identifier>
</Patient>
</contained>
</Patient>
</resource>
</entry>
</Bundle>
下面的兩個查詢是我嘗試使其與 XDocument 一起使用,但都不起作用。它們運行時沒有錯誤,但不會移除患者。
xdoc.Root.Descendants("entry").Descendants("resource").Descendants("Patient").Descendants("contained").Descendants("Patient").Where(x => x.Element("identifier").Element("system").Attribute("value").Value.Contains("remove-this-Patient")).Remove();
xdoc.Root.Descendants("entry").Descendants("resource").Descendants("Patient").Descendants("contained").Descendants("Patient").Where(x => (string)x.Descendants("identifier").Where(y=> ("system").Attribute("value")=="https://example.com/remove-this-Patient").Remove();
uj5u.com熱心網友回復:
請嘗試以下基于 XSLT 轉換的解決方案。
下面的 XSLT 遵循所謂的身份轉換模式。
第二個模板根據@value屬性中“remove-this-Patient”值的存在洗掉不需要的“/Bundle/entry/resource/Patient/contained/Patient” XML 元素。
輸入 XML
<Bundle>
<id value="xxxx"/>
<entry>
<fullUrl value="xxxxxxx"/>
<resource>
<Patient>
<id value="xxxx"/>
<contained>
<Practitioner>
<id value="xx"/>
</Practitioner>
</contained>
<contained>
<Patient>
<id value="xxxx"/>
<identifier>
<type>
<coding>
</coding>
</type>
<system value="http://example.com/remove-this-Patient"/>
<value value="xxx"/>
</identifier>
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-value"/>
<value value="xxx"/>
</identifier>
</Patient>
</contained>
<contained>
<Patient>
<id value="xxxx"/>
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-thing"/>
<value value="xxx"/>
</identifier>
<identifier>
<type>
<coding>
</coding>
</type>
<system value="https://example.com/some-other-value"/>
<value value="xxx"/>
</identifier>
</Patient>
</contained>
</Patient>
</resource>
</entry>
</Bundle>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Bundle/entry/resource/Patient/contained/Patient[identifier/system[contains(@value, 'remove-this-Patient')]]">
</xsl:template>
</xsl:stylesheet>
輸出 XML
<Bundle>
<id value="xxxx" />
<entry>
<fullUrl value="xxxxxxx" />
<resource>
<Patient>
<id value="xxxx" />
<contained>
<Practitioner>
<id value="xx" />
</Practitioner>
</contained>
<contained />
<contained>
<Patient>
<id value="xxxx" />
<identifier>
<type>
<coding />
</type>
<system value="https://example.com/some-other-thing" />
<value value="xxx" />
</identifier>
<identifier>
<type>
<coding />
</type>
<system value="https://example.com/some-other-value" />
<value value="xxx" />
</identifier>
</Patient>
</contained>
</Patient>
</resource>
</entry>
</Bundle>
uj5u.com熱心網友回復:
嘗試以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication5
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
string id = "xxxx";
List<XElement> possibleDeletes = doc.Descendants().Where(x => (x.Element("identifier") != null) && (string)x.Element("id").Attribute("value") == id).ToList();
foreach (XElement possibleDelete in possibleDeletes)
{
List<XElement> identifiers = possibleDelete.Elements("identifier").ToList();
foreach (XElement identifier in identifiers)
{
if (((string)identifier.Element("system").Attribute("value")).Contains("remove")) identifier.Remove();
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371740.html
上一篇:將SQL查詢轉換為Linq,它使用復雜的sum和groupby
下一篇:使用linq過濾繁瑣的物件串列
