Highlighted
friedman16
New Member.
475 views

Finding XML tag names

How do I loop through a set of XML tags where I can put the XML tag name into a local variable and put he value of the tag into a different local variable?

I have an XML set that I am looping through. Although the following example shows only one complete Appointment XML set, there could be more than one (as shown with the dots).

<Appt>
<Appointment>
<OSUapptPrimary>TRUE</OSUapptPrimary>
<OSUapptAction>new</OSUapptAction>
<OSUapptActionCd>CNV</OSUapptActionCd>
<OSUapptActionDt>07/07/2010</OSUapptActionDt>
<OSUapptActionReason>CNV</OSUapptActionReason>
<OSUapptClassIndc>F</OSUapptClassIndc>
<OSUapptDeptID>18001</OSUapptDeptID>
<OSUapptEffdt>01/07/2011</OSUapptEffdt>
<OSUapptEmplStatus>A</OSUapptEmplStatus>
<OSUapptJobcode>31806</OSUapptJobcode>
<OSUapptLoc>2731</OSUapptLoc>
<OSUapptPstnNbr>00033425</OSUapptPstnNbr>
<OSUapptRecordNum>0</OSUapptRecordNum>
<OSUapptRegTemp>R</OSUapptRegTemp>
<OSUapptRptsTo>00022295</OSUapptRptsTo>
<OSUapptSalaryPlan>FAC</OSUapptSalaryPlan>
<OSUapptStdHours>21</OSUapptStdHours>
<OSUsupervisorID>200002386</OSUsupervisorID>
</Appointment>
<Appointment>
...
</Appointment>
<Appointment>
...
</Appointment>
</Appt>


I can loop through the each appointment individually with this code:
<do-set-local-variable name="lv-Appointments" scope="policy">
<arg-node-set>
<token-xpath expression="$lv-Newchanges/Appt/Appointment"/>
</arg-node-set>
</do-set-local-variable>
<do-for-each>
<arg-node-set>
<token-local-variable name="lv-Appointments"/>
</arg-node-set>
<< what do I do here? >>
</arg-actions>
</do-for-each>


how do I loop through the xml tags within the Appointment tags so that I can pull out the actual tag name and the tag value?
Labels (1)
Tags (3)
0 Likes
7 Replies
Knowledge Partner
Knowledge Partner

Re: Finding XML tag names

On 2/14/2019 12:24 PM, friedman16 wrote:
>
> How do I loop through a set of XML tags where I can put the XML tag name
> into a local variable and put he value of the tag into a different local
> variable?
>
> I have an XML set that I am looping through. Although the following
> example shows only one complete Appointment XML set, there could be more
> than one (as shown with the dots).
>
>
> Code:
> --------------------
> <Appt>
> <Appointment>
> <OSUapptPrimary>TRUE</OSUapptPrimary>
> <OSUapptAction>new</OSUapptAction>
> <OSUapptActionCd>CNV</OSUapptActionCd>
> <OSUapptActionDt>07/07/2010</OSUapptActionDt>
> <OSUapptActionReason>CNV</OSUapptActionReason>
> <OSUapptClassIndc>F</OSUapptClassIndc>
> <OSUapptDeptID>18001</OSUapptDeptID>
> <OSUapptEffdt>01/07/2011</OSUapptEffdt>
> <OSUapptEmplStatus>A</OSUapptEmplStatus>
> <OSUapptJobcode>31806</OSUapptJobcode>
> <OSUapptLoc>2731</OSUapptLoc>
> <OSUapptPstnNbr>00033425</OSUapptPstnNbr>
> <OSUapptRecordNum>0</OSUapptRecordNum>
> <OSUapptRegTemp>R</OSUapptRegTemp>
> <OSUapptRptsTo>00022295</OSUapptRptsTo>
> <OSUapptSalaryPlan>FAC</OSUapptSalaryPlan>
> <OSUapptStdHours>21</OSUapptStdHours>
> <OSUsupervisorID>200002386</OSUsupervisorID>
> </Appointment>
> <Appointment>
> ...
> </Appointment>
> <Appointment>
> ...
> </Appointment>
> </Appt>
>
> --------------------
>
>
> I can loop through the each appointment individually with this code:
>
> Code:
> --------------------
> <do-set-local-variable name="lv-Appointments" scope="policy">
> <arg-node-set>
> <token-xpath expression="$lv-Newchanges/Appt/Appointment"/>
> </arg-node-set>
> </do-set-local-variable>
> <do-for-each>
> <arg-node-set>
> <token-local-variable name="lv-Appointments"/>
> </arg-node-set>
> << what do I do here? >>
> </arg-actions>
> </do-for-each>
> --------------------
>
>
> how do I loop through the xml tags within the Appointment tags so that I
> can pull out the actual tag name and the tag value?


Loop over $lv-appointment

and then I think XPATH of name($current-node)
$current-node would be the value of the line. name($current-node) should
be the name of the node.

When in doubt, look at the XPath 1.0 RFC:
https://www.w3.org/TR/1999/REC-xpath-19991116/

0 Likes
Knowledge Partner
Knowledge Partner

Re: Finding XML tag names

friedman16;2495350 wrote:
How do I loop through a set of XML tags where I can put the XML tag name into a local variable and put he value of the tag into a different local variable?

I have an XML set that I am looping through. Although the following example shows only one complete Appointment XML set, there could be more than one (as shown with the dots).

<Appt>
<Appointment>
<OSUapptPrimary>TRUE</OSUapptPrimary>
<OSUapptAction>new</OSUapptAction>
<OSUapptActionCd>CNV</OSUapptActionCd>
<OSUapptActionDt>07/07/2010</OSUapptActionDt>
<OSUapptActionReason>CNV</OSUapptActionReason>
<OSUapptClassIndc>F</OSUapptClassIndc>
<OSUapptDeptID>18001</OSUapptDeptID>
<OSUapptEffdt>01/07/2011</OSUapptEffdt>
<OSUapptEmplStatus>A</OSUapptEmplStatus>
<OSUapptJobcode>31806</OSUapptJobcode>
<OSUapptLoc>2731</OSUapptLoc>
<OSUapptPstnNbr>00033425</OSUapptPstnNbr>
<OSUapptRecordNum>0</OSUapptRecordNum>
<OSUapptRegTemp>R</OSUapptRegTemp>
<OSUapptRptsTo>00022295</OSUapptRptsTo>
<OSUapptSalaryPlan>FAC</OSUapptSalaryPlan>
<OSUapptStdHours>21</OSUapptStdHours>
<OSUsupervisorID>200002386</OSUsupervisorID>
</Appointment>
<Appointment>
...
</Appointment>
<Appointment>
...
</Appointment>
</Appt>


I can loop through the each appointment individually with this code:
<do-set-local-variable name="lv-Appointments" scope="policy">
<arg-node-set>
<token-xpath expression="$lv-Newchanges/Appt/Appointment"/>
</arg-node-set>
</do-set-local-variable>
<do-for-each>
<arg-node-set>
<token-local-variable name="lv-Appointments"/>
</arg-node-set>
<< what do I do here? >>
</arg-actions>
</do-for-each>


how do I loop through the xml tags within the Appointment tags so that I can pull out the actual tag name and the tag value?



Something like this will get you started.


<rule>
<description>pt</description>
<conditions>
<and/>
</conditions>
<actions>
<do-set-local-variable name="testdata" scope="policy">
<arg-string>
<token-text xml:space="preserve"><Appt>
<Appointment>
<OSUapptPrimary>TRUE</OSUapptPrimary>
<OSUapptAction>new</OSUapptAction>
<OSUapptActionCd>CNV</OSUapptActionCd>
<OSUapptActionDt>07/07/2010</OSUapptActionDt>
<OSUapptActionReason>CNV</OSUapptActionReason>
<OSUapptClassIndc>F</OSUapptClassIndc>
<OSUapptDeptID>18001</OSUapptDeptID>
<OSUapptEffdt>01/07/2011</OSUapptEffdt>
<OSUapptEmplStatus>A</OSUapptEmplStatus>
<OSUapptJobcode>31806</OSUapptJobcode>
<OSUapptLoc>2731</OSUapptLoc>
<OSUapptPstnNbr>00033425</OSUapptPstnNbr>
<OSUapptRecordNum>0</OSUapptRecordNum>
<OSUapptRegTemp>R</OSUapptRegTemp>
<OSUapptRptsTo>00022295</OSUapptRptsTo>
<OSUapptSalaryPlan>FAC</OSUapptSalaryPlan>
<OSUapptStdHours>21</OSUapptStdHours>
<OSUsupervisorID>200002386</OSUsupervisorID>
</Appointment>
</Appt></token-text>
</arg-string>
</do-set-local-variable>
<do-set-local-variable name="testdatans" scope="policy">
<arg-node-set>
<token-xml-parse>
<token-local-variable name="testdata"/>
</token-xml-parse>
</arg-node-set>
</do-set-local-variable>
<do-set-local-variable name="lv-Appointments" scope="policy">
<arg-node-set>
<token-xpath expression="$testdatans/Appt/Appointment/*"/>
</arg-node-set>
</do-set-local-variable>
<do-for-each>
<arg-node-set>
<token-local-variable name="lv-Appointments"/>
</arg-node-set>
<arg-actions>
<do-trace-message>
<arg-string>
<token-text xml:space="preserve">Thing Name is </token-text>
<token-xpath expression="name($current-node)"/>
<token-text xml:space="preserve"> Thing Value is </token-text>
<token-xpath expression="$current-node"/>
</arg-string>
</do-trace-message>
</arg-actions>
</do-for-each>
</actions>
</rule>


You probably want to nest for-loop over the <appointment> nodes, with this as an inner loop. Note the XPath to select the nodes below the <appointment> node for processing in this loop.
0 Likes
Knowledge Partner
Knowledge Partner

Re: Finding XML tag names

> Code:
> --------------------
>
> <rule>
> <description>pt</description>
> <conditions>
> <and/>
> </conditions>
> <actions>
> <do-set-local-variable name="testdata" scope="policy">
> <arg-string>
> <token-text xml:space="preserve"><Appt>
> <Appointment>
> <OSUapptPrimary>TRUE</OSUapptPrimary>
> <OSUapptAction>new</OSUapptAction>
> <OSUapptActionCd>CNV</OSUapptActionCd>
> <OSUapptActionDt>07/07/2010</OSUapptActionDt>
> <OSUapptActionReason>CNV</OSUapptActionReason>
> <OSUapptClassIndc>F</OSUapptClassIndc>
> <OSUapptDeptID>18001</OSUapptDeptID>
> <OSUapptEffdt>01/07/2011</OSUapptEffdt>
> <OSUapptEmplStatus>A</OSUapptEmplStatus>
> <OSUapptJobcode>31806</OSUapptJobcode>
> <OSUapptLoc>2731</OSUapptLoc>
> <OSUapptPstnNbr>00033425</OSUapptPstnNbr>
> <OSUapptRecordNum>0</OSUapptRecordNum>
> <OSUapptRegTemp>R</OSUapptRegTemp>
> <OSUapptRptsTo>00022295</OSUapptRptsTo>
> <OSUapptSalaryPlan>FAC</OSUapptSalaryPlan>
> <OSUapptStdHours>21</OSUapptStdHours>
> <OSUsupervisorID>200002386</OSUsupervisorID>
> </Appointment>
> </Appt></token-text>
> </arg-string>
> </do-set-local-variable>
> <do-set-local-variable name="testdatans" scope="policy">
> <arg-node-set>
> <token-xml-parse>
> <token-local-variable name="testdata"/>
> </token-xml-parse>
> </arg-node-set>
> </do-set-local-variable>
> <do-set-local-variable name="lv-Appointments" scope="policy">
> <arg-node-set>
> <token-xpath expression="$testdatans/Appt/Appointment/*"/>
> </arg-node-set>
> </do-set-local-variable>
> <do-for-each>
> <arg-node-set>
> <token-local-variable name="lv-Appointments"/>
> </arg-node-set>
> <arg-actions>
> <do-trace-message>
> <arg-string>
> <token-text xml:space="preserve">Thing Name is </token-text>
> <token-xpath expression="name($current-node)"/>
> <token-text xml:space="preserve"> Thing Value is </token-text>
> <token-xpath expression="$current-node"/>
> </arg-string>
> </do-trace-message>
> </arg-actions>
> </do-for-each>
> </actions>
> </rule>
>
> --------------------
>
>
> You probably want to nest for-loop over the <appointment> nodes, with
> this as an inner loop. Note the XPath to select the nodes below the
> <appointment> node for processing in this loop.


Oh sure, like a code example would help. That is just showing off. 🙂

0 Likes
Knowledge Partner
Knowledge Partner

Re: Finding XML tag names

geoffc;2495372 wrote:
> Code:
> --------------------
>
> <rule>
> <description>pt</description>
> <conditions>
> <and/>
> </conditions>
> <actions>
> <do-set-local-variable name="testdata" scope="policy">
> <arg-string>
> <token-text xml:space="preserve"><Appt>
> <Appointment>
> <OSUapptPrimary>TRUE</OSUapptPrimary>
> <OSUapptAction>new</OSUapptAction>
> <OSUapptActionCd>CNV</OSUapptActionCd>
> <OSUapptActionDt>07/07/2010</OSUapptActionDt>
> <OSUapptActionReason>CNV</OSUapptActionReason>
> <OSUapptClassIndc>F</OSUapptClassIndc>
> <OSUapptDeptID>18001</OSUapptDeptID>
> <OSUapptEffdt>01/07/2011</OSUapptEffdt>
> <OSUapptEmplStatus>A</OSUapptEmplStatus>
> <OSUapptJobcode>31806</OSUapptJobcode>
> <OSUapptLoc>2731</OSUapptLoc>
> <OSUapptPstnNbr>00033425</OSUapptPstnNbr>
> <OSUapptRecordNum>0</OSUapptRecordNum>
> <OSUapptRegTemp>R</OSUapptRegTemp>
> <OSUapptRptsTo>00022295</OSUapptRptsTo>
> <OSUapptSalaryPlan>FAC</OSUapptSalaryPlan>
> <OSUapptStdHours>21</OSUapptStdHours>
> <OSUsupervisorID>200002386</OSUsupervisorID>
> </Appointment>
> </Appt></token-text>
> </arg-string>
> </do-set-local-variable>
> <do-set-local-variable name="testdatans" scope="policy">
> <arg-node-set>
> <token-xml-parse>
> <token-local-variable name="testdata"/>
> </token-xml-parse>
> </arg-node-set>
> </do-set-local-variable>
> <do-set-local-variable name="lv-Appointments" scope="policy">
> <arg-node-set>
> <token-xpath expression="$testdatans/Appt/Appointment/*"/>
> </arg-node-set>
> </do-set-local-variable>
> <do-for-each>
> <arg-node-set>
> <token-local-variable name="lv-Appointments"/>
> </arg-node-set>
> <arg-actions>
> <do-trace-message>
> <arg-string>
> <token-text xml:space="preserve">Thing Name is </token-text>
> <token-xpath expression="name($current-node)"/>
> <token-text xml:space="preserve"> Thing Value is </token-text>
> <token-xpath expression="$current-node"/>
> </arg-string>
> </do-trace-message>
> </arg-actions>
> </do-for-each>
> </actions>
> </rule>
>
> --------------------
>
>
> You probably want to nest for-loop over the <appointment> nodes, with
> this as an inner loop. Note the XPath to select the nodes below the
> <appointment> node for processing in this loop.


Oh sure, like a code example would help. That is just showing off. 🙂


Yeah, well, it's not a complete answer. The rest is left as an exercise for the reader.
0 Likes
Knowledge Partner
Knowledge Partner

Re: Finding XML tag names

>>> You probably want to nest for-loop over the <appointment> nodes, with
>>> this as an inner loop. Note the XPath to select the nodes below the
>>> <appointment> node for processing in this loop.

>>
>> Oh sure, like a code example would help. That is just showing off. 🙂

>
> Yeah, well, it's not a complete answer. The rest is left as an exercise
> for the reader.


I suppose that makes it fair. Alright, apology accepted. 🙂

0 Likes
rrawson Honored Contributor.
Honored Contributor.

Re: Finding XML tag names

geoffc;2495382 wrote:
>>> You probably want to nest for-loop over the <appointment> nodes, with
>>> this as an inner loop. Note the XPath to select the nodes below the
>>> <appointment> node for processing in this loop.

>>
>> Oh sure, like a code example would help. That is just showing off. 🙂

>
> Yeah, well, it's not a complete answer. The rest is left as an exercise
> for the reader.


I suppose that makes it fair. Alright, apology accepted. 🙂


In Pseudo code, I think it's something like this:

for-each(xpath("//Appointments"))
for-each(xpath("*"))
element-name = xpath("$current-node/name()")
element-value = xpath("$current-node/text()")
0 Likes
Knowledge Partner
Knowledge Partner

Re: Finding XML tag names

On 2/16/2019 2:06 PM, rrawson wrote:
>
> geoffc;2495382 Wrote:
>>>>> You probably want to nest for-loop over the <appointment> nodes,

>> with
>>>>> this as an inner loop. Note the XPath to select the nodes below the
>>>>> <appointment> node for processing in this loop.
>>>>
>>>> Oh sure, like a code example would help. That is just showing off.

>> 🙂
>>>
>>> Yeah, well, it's not a complete answer. The rest is left as an

>> exercise
>>> for the reader.

>>
>> I suppose that makes it fair. Alright, apology accepted. 🙂

>
> In Pseudo code, I think it's something like this:
>
> for-each(xpath("//Appointments"))
> for-each(xpath("*"))
> element-name = xpath("$current-node/name()")
> element-value = xpath("$current-node/text()")


David had a pretty straightforward approach. I am just giving him a hard
time because it is fun.


0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.