-1

This is a XML code snippet:

<testcase name="T.3.03.02">
    <cmd>CMD_EXPORT_RAM_KEY</cmd>
    <sreg_pre>40</sreg_pre>
    <sreg_pre_bitmask>ff</sreg_pre_bitmask>
    <sreg_post>40</sreg_post>
    <sreg_post_bitmask>ff</sreg_post_bitmask>
    <erc>ERC_NO_ERROR</erc>
    <testvector>
        <parameter name="UID" type="info">000000000000000000000000000002</parameter>
        <parameter name="UID'" type="info">000000000000000000000000000002</parameter>
        <parameter name="KeyId" type="info">0e</parameter>
        <parameter name="Key" type="info">0f0e0d0c0b0a09080706050403020100</parameter>
        <parameter name="AuthId" type="info">00</parameter>
        <parameter name="KeyAuth" type="info">2b7e151628aed2a6abf7158809cf4f3c</parameter>
        <parameter name="Old counter value of updated key slot" type="info">0000000</parameter>
        <parameter name="New counter value C'" type="info">0000000</parameter>
        <parameter name="Protection flags F'" type="info">00</parameter>
        <parameter name="M1" type="output">000000000000000000000000000002e0</parameter>
        <parameter name="M2" type="output">152876f29dc7ca8d18e38d70374492b05d908c8c584a0409849a553c75254def</parameter>
        <parameter name="M3" type="output">bc6e79bc4458339174fc80fb08b83188</parameter>
        <parameter name="M4" type="output">000000000000000000000000000002e07783b86ae87b87e3ca12809c2df75fae</parameter>
        <parameter name="M5" type="output">c8fcc8859c69c8bd840ce8e24c5114e9</parameter>
    </testvector>
    <precondition>RAM_KEY_PLAIN = 1; RAM_KEY_EMPTY = 0</precondition>
    <description>Export plain RAM_KEY with external debugger attached; Note: The security flags SECURE_BOOT_PROTECTION and DEBUGGER_PROTECTION of the key SECRET_KEY are inherited from MASTER_ECU_KEY.</description>
</testcase>

I want to access all "parameter name="Key" type="info" values.

How do I access these values conditionally if the condition <cmd>CMD_EXPORT_RAM_KEY(second line in XML)</cmd> is valid.

In this XML file there are also other commands (<cmd> lines) also with the "Key" parameter, but in these cases I don't want to get the key-values.

I didn't get it running.

Can anyone help me with some ideas?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
SJ_68
  • 1

3 Answers3

0

Would something like this work?

  doc = Nokogiri::parse(File.read( "data.xml" ))
  check = doc.xpath( "//cmd" ).select{|el| el.children[0].text == "CMD_EXPORT_RAM_KEY" }
  puts "Check: %i" % check.size
  if(check.size == 0)
    ## Do stuff here
  end
0

Try the below XPath with Nokogiri:

//testcase/cmd[text()='CMD_EXPORT_RAM_KEY']/../testvector/parameter[@name="Key" and @type="info"]

Of course, you can parameterize the CMD_EXPORT_RAM_KEY and @name/@type values.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Vimalraj Selvam
  • 2,155
  • 3
  • 23
  • 52
0

The trick is to locate the specific ones you want using a selector, then narrow down further if necessary.

Using the CSS selector 'parameter[@name="Key"][@type="info"]' Nokogiri easily finds the single occurrence in your sample. If there were more, then more would be returned:

require 'nokogiri'

doc = Nokogiri::XML(<<EOT)
<testcase name="T.3.03.02">
    <testvector>
        <parameter name="UID" type="info">000000000000000000000000000002</parameter>
        <parameter name="Key" type="info">0f0e0d0c0b0a09080706050403020100</parameter>
    </testvector>
</testcase>
EOT

doc.search('parameter[@name="Key"][@type="info"]').map(&:content)
# => ["0f0e0d0c0b0a09080706050403020100"]

I used CSS because it looks less like line noise than the equivalent XPath selector would.

Also, when supplying sample data, reduce it to the bare minimum necessary to test the code. Anything beyond that wastes our time, and, if it's too much, can actually cause you to get no answers because nobody wants to wade through that.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303