Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath: select multiple child element attributes from xml

Tags:

xml

xpath

sas

Having trouble getting the desired output... maybe it's not possible but I'm new to XPath selectors.

I have XML like:

<submission>
    <component type="X">
        <audit>
            <measures>
                <measure id="xyz">
                    <reported>false</reported>
                    <benefit>false</benefit>
                    <data-elements>
                        <data-element id="rate1">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                        <data-element id="rate2">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                        <data-element id="rate3">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                        <data-element id="rate4">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                    </data-elements>
                </measure>
                <measure id="abc">
                    <reported>false</reported>
                    <benefit>false</benefit>
                    <data-elements>
                        <data-element id="rate5">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                        <data-element id="rate6">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                        <data-element id="rate7">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                        <data-element id="rate8">
                            <reportable>false</reportable>
                            <comment/>
                        </data-element>
                    </data-elements>
                </measure>
            </measures>
        </audit>
    </component>
</submission>

I'm trying to access the attributes with XPath selectors using a XMLMAP like this:

<?xml version="1.0" ?>
<SXLEMAP version="1.2">
  <TABLE name="AUDIT">  
    <TABLE-PATH syntax="XPath">  
       /submission/component/audit/measures/measure
     </TABLE-PATH>

    <COLUMN name="MEASURE" retain="YES">  
      <PATH syntax="XPath">  
         /submission/component/audit/measures/measure@id
        </PATH>
        <TYPE>character</TYPE>
        <DATATYPE>STRING</DATATYPE>
        <LENGTH>30</LENGTH>
     </COLUMN>

    <COLUMN name="RATES">  
      <PATH syntax="XPath">  
       //submission/component/audit/measures/measure/data-elements/data-element/@id
        </PATH>
        <TYPE>character</TYPE>
        <DATATYPE>STRING</DATATYPE>
        <LENGTH>20</LENGTH>
     </COLUMN>

    <COLUMN name="REPORT">  
      <PATH syntax="XPath">  
       /submission/component/audit/measures/measure/data-elements/data-element/reportable
        </PATH>
        <TYPE>character</TYPE>
        <DATATYPE>STRING</DATATYPE>
        <LENGTH>20</LENGTH>
     </COLUMN>

My output looks like:

   MEASURE                           RATES                   REPORT

   xyz                               rate4                   false
   abc                               rate8                   false

I want my output to get ALL the rates, not just the last rate, like:

   MEASURE                           RATES                   REPORT

   xyz                               rate1                   false
   xyz                               rate2                   false
   xyz                               rate3                   false
   xyz                               rate4                   false
   abc                               rate5                   false
   abc                               rate6                   false
   abc                               rate7                   false
   abc                               rate8                   false

Is this possible using XMLMAP or do I need something else?

Thanks in advance for your input!

****edit***********************************************;

Here is a basic SAS program that pulls the xml in and prints the output:

* set destination for output *;
LIBNAME DATA '<filepath>';

* set location of xml document and xml_map *;
LIBNAME XFILE XML '<filepath>\stack_example.xml' 
          XMLMAP='<filepath>\stack_import_map.xml';


* check import *;
PROC PRINT DATA= XFILE.AUDIT;
RUN;
like image 254
xxsoundgirlxx Avatar asked Feb 18 '26 18:02

xxsoundgirlxx


1 Answers

I figured it out...

In my table path I need to get down to the line level where I want a new observation (row) to start, like:

<TABLE name="AUDIT">  
        <TABLE-PATH syntax="XPath">  
           /submission/component/audit/measures/measure/data-elements/data-element
         </TABLE-PATH>

This will now select all the data-elements I want.

like image 148
xxsoundgirlxx Avatar answered Feb 21 '26 11:02

xxsoundgirlxx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!