2

如何获取rc搜索返回的元素的xpath

f=File.open('/media/cc.xml')
doc = Nokogiri::XML f

rc = doc.search('realmCode')
[#<Nokogiri::XML::Element:0x15a4d714 name="realmCode" namespace=#<Nokogiri::XML::Namespace:0x15a4dafc href="urn:hl7-org:v3"> attributes=[#<Nokogiri::XML::Attr:0x15a4d5c0 name="code" value="US">]>]

有没有办法从中提取xpath rc

用xml更新

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/ccda.xsl"?>
<ClinicalDocument xmlns:sdtc="urn:hl7-org:sdtc" xmlns:vocdo="urn:hl7-org:v3/voc" xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mif="urn:hl7-org:v3/mif">
  <realmCode code="US"/>
  <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
  <templateId root="2.16.840.1.113883.10.20.22.1.1"/>
  <templateId root="2.16.840.1.113883.10.20.22.1.2"/>
  <id root="2.16.840.1.113883.19.5.99999.1" extension="Test CCDA"/>
  <code codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" code="34133-9" displayName="Summarization of Episode Note"/>
  <title>Continuity of Care Document (C-CDA)</title>
  <effectiveTime value="20130809043133+0000"/>
  <confidentialityCode codeSystem="2.16.840.1.113883.5.25" codeSystemName="HL7 Confidentiality" code="N" displayName="Normal"/>
  <languageCode code="en-US"/>
  <recordTarget>
    <patientRole>
      <id root="2.16.840.1.113883.4.6" extension="1"/>
      <id root="2.16.840.1.113883.4.1" extension="123-101-5230"/>
      <addr use="HP">
        <streetAddressLine>1357 Amber Drive</streetAddressLine>
        <city nullFlavor="UNK"/>
        <state nullFlavor="UNK"/>
        <postalCode>97006</postalCode>
        <country nullFlavor="UNK"/>
      </addr>
      <telecom value="3545345" use="HP"/>
      <patient>
        <name use="L">
          <given qualifier="BR">test</given>
          <family qualifier="CL">overall</family>
          <prefix qualifier="IN">Mr</prefix>
        </name>
        <administrativeGenderCode codeSystem="2.16.840.1.113883.5.1" codeSystemName="HL7 AdministrativeGender" code="M" displayName="Male"/>
        <birthTime value="19770429"/>
        <raceCode codeSystem="2.16.840.1.113883.6.238" codeSystemName="Race and Ethnicity - CDC" code="2028-9" displayName="Asian"/>
        <ethnicGroupCode codeSystem="2.16.840.1.113883.6.238" codeSystemName="Race and Ethnicity - CDC" code="2135-2" displayName="Hispanic or Latino"/>
        <languageCommunication>
          <languageCode code="eng"/>
          <preferenceInd value="true"/>
        </languageCommunication>
      </patient>
    </patientRole>
  </recordTarget>
</ClinicalDocument>
4

1 回答 1

3

rc 不是一个元素——它是一个匹配元素的数组

results = rc.map do |node|
  Nokogiri::CSS.xpath_for node.css_path
end

p results

或者,如果您知道只有一个匹配元素:

xpath = Nokogiri::CSS.xpath_for rc[0].css_path

请注意,xpath_for 返回一个数组,因此您需要提取数组的第一个元素:

xpath.first
于 2013-08-09T06:26:58.807 回答