title: Extract XML Tag Values (Based on a Flag) Using Batch
body: <p>I am a beginner in writing batch scripts and your help on this is very much apreciated. </p>
  
  <p>Below is the xml and I need to extract all names whose flag is "on" to a txt file. There are several other category tag instances.</p>
  
  <pre><code>&lt;Head&gt;
     &lt;Category
        name="RIVERTD"
        flag="on"
        location="SG002"&gt;
      &lt;/Category&gt;
     &lt;Category
        name="BRETRED"
        flag="on"
        location="IT213"&gt;
      &lt;/Category&gt;
     &lt;Category
        name="AMERAND"
        flag="off"
        location="US212"&gt;
     &lt;/Category&gt;
  &lt;/Head&gt;
  </code></pre>
  
  <p>So, the output I'm looking is as below</p>
  
  <pre><code>RIVERTD
  BRETRED
  </code></pre>
  
  <p>I tried using below code </p>
  
  <pre><code>@echo off
  setlocal disableDelayedExpansion
  set input="CP.xml"
  set output="Names.txt"
  
  if exist %output% del %output%
  for /f "delims=" %%A in ('findstr /n /c:"name=" %input%') do (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    call :parseLine
    endlocal
  )
  type %output%
  exit /b
  
  :parseLine
  set "ln2=!ln:*name=!"
  if "!ln2!"=="!ln!" exit /b
  for /f tokens^=2^ delims^=^" %%B in ("!ln2!") do (
    setlocal disableDelayedExpansion
    &gt;&gt;%output% echo(%%B
    endlocal
  )
  set "ln=!ln2!"
  goto :parseLine
  </code></pre>
  
  <p>This gives me the result</p>
  
  <pre><code>RIVERTD
  BRETRED
  AMERAND
  </code></pre>
  
  <p>However this code doesn't filter the names based on flag. I'm a beginner. Kindly help to add filter based on flag. Many thanks.</p>