FOR XML PATH - Return Only Respective Result

I want the query below to only insert the label for its respective regionID – instead it currently iterates through all of them.  Also, can I include the !CDATA when it’s embedded in the larger FOR XML PATH


I want the query below to only insert the <label> for its respective region – instead it currently iterates through all of them.  Also, can I include the !CDATA when it’s embedded in the larger FOR XML PATH query?

The output  is below the query code:

Thanks in advance,
Rebecca



<plan id="1871" garage="left">
  <regions>
    <region id="18711" tag="base" livable="true" measurable="true" optional="false">
      <label textStyle="labelStandard">PLAN 1871</label>   (this is where I only want it to output the title for its respective region)
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="0" default="false">
        <line>
          <point x="38.4989" y="59.9989" />
          <point x="26.5000" y="60.0000" />
        </line>
        <line>
          <point x="38.4989" y="60.0436" />
          <point x="38.5000" y="9.0000" />
        </line>
        <line>
          <point x="23.4999" y="9.0000" />
          <point x="23.4999" y="23.3333" />
        </line>
        <line>
          <point x="38.5000" y="9.0000" />
          <point x="23.4999" y="9.0000" />
        </line>
        <line>
          <point x="26.5000" y="76.0001" />
          <point x="26.5000" y="60.0000" />
        </line>
        <line>
          <point x="3.5000" y="76.0000" />
          <point x="26.5000" y="76.0001" />
        </line>
        <line>
          <point x="3.5000" y="23.3076" />
          <point x="3.5000" y="76.0000" />
        </line>
      </version>
    </region>
    <region id="18712" tag="ac" livable="false" measurable="false" optional="false">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="0" default="false">
        <line>
          <point x="0.0000" y="63.9256" />
          <point x="0.0000" y="60.9256" />
        </line>
        <line>
          <point x="3.0000" y="63.9256" />
          <point x="0.0000" y="63.9256" />
        </line>
        <line>
          <point x="3.0000" y="60.9256" />
          <point x="3.0000" y="63.9256" />
        </line>
        <line>
          <point x="0.0000" y="60.9256" />
          <point x="3.0000" y="60.9256" />
        </line>
      </version>
    </region>
    <region id="18713" tag="garage" livable="false" measurable="true" optional="false">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="0" default="false">
        <line>
          <point x="23.5000" y="3.0000" />
          <point x="23.4999" y="23.3333" />
        </line>
        <line>
          <point x="3.5000" y="3.0000" />
          <point x="23.5000" y="3.0000" />
        </line>
        <line>
          <point x="3.5000" y="23.3333" />
          <point x="3.5000" y="3.0000" />
        </line>
        <line>
          <point x="23.4999" y="23.3333" />
          <point x="3.5000" y="23.3333" />
        </line>
        <line>
          <point x="0.2500" y="16.7500" />
          <point x="3.2501" y="16.7500" />
        </line>
      </version>
    </region>
    <region id="18714" tag="porch" livable="false" measurable="true" optional="false">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="0" default="false">
        <line>
          <point x="23.5000" y="3.0000" />
          <point x="22.4999" y="3.0000" />
        </line>
        <line>
          <point x="23.4999" y="9.0000" />
          <point x="23.5000" y="3.0000" />
        </line>
        <line>
          <point x="38.5000" y="9.0000" />
          <point x="23.4999" y="9.0000" />
        </line>
        <line>
          <point x="38.5000" y="2.0000" />
          <point x="38.5000" y="9.0000" />
        </line>
        <line>
          <point x="22.5000" y="2.0000" />
          <point x="38.5000" y="2.0000" />
        </line>
        <line>
          <point x="22.5000" y="3.0000" />
          <point x="22.5000" y="2.0000" />
        </line>
      </version>
    </region>
    <region id="18715" tag="patio" livable="false" measurable="true" optional="false">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="0" default="false">
        <line>
          <point x="26.5000" y="69.6667" />
          <point x="38.5000" y="69.6694" />
        </line>
        <line>
          <point x="26.5000" y="60.0436" />
          <point x="26.5000" y="69.6667" />
        </line>
        <line>
          <point x="38.4989" y="60.0436" />
          <point x="26.5000" y="60.0436" />
        </line>
        <line>
          <point x="38.4996" y="69.6694" />
          <point x="38.4989" y="60.0436" />
        </line>
      </version>
    </region>
    <region id="18716" tag="extpatio" livable="false" measurable="true" optional="true">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="1" default="false">
        <line>
          <point x="26.5000" y="60.0436" />
          <point x="26.5000" y="76.0001" />
        </line>
        <line>
          <point x="38.4989" y="60.0436" />
          <point x="26.5000" y="60.0436" />
        </line>
        <line>
          <point x="38.5000" y="76.0001" />
          <point x="38.4989" y="60.0436" />
        </line>
        <line>
          <point x="26.5000" y="76.0001" />
          <point x="38.5000" y="76.0001" />
        </line>
      </version>
    </region>
    <region id="18717" tag="extporch" livable="false" measurable="true" optional="true">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="0" default="false">
        <line>
          <point x="22.5000" y="2.0000" />
          <point x="38.5000" y="2.0000" />
        </line>
        <line>
          <point x="22.5000" y="0.0000" />
          <point x="22.5000" y="2.0000" />
        </line>
        <line>
          <point x="38.5000" y="0.0000" />
          <point x="38.5000" y="2.0000" />
        </line>
        <line>
          <point x="22.5000" y="0.0000" />
          <point x="38.5000" y="0.0000" />
        </line>
      </version>
    </region>
    <region id="18718" tag="extgarage" livable="false" measurable="true" optional="false">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="1" default="false">
        <line>
          <point x="22.5000" y="3.0000" />
          <point x="3.5000" y="3.0000" />
        </line>
        <line>
          <point x="22.5000" y="1.0000" />
          <point x="22.5000" y="3.0000" />
        </line>
        <line>
          <point x="3.5000" y="1.0000" />
          <point x="3.5000" y="3.0000" />
        </line>
        <line>
          <point x="3.5000" y="1.0000" />
          <point x="22.5000" y="1.0000" />
        </line>
      </version>
    </region>
    <region id="18719" tag="servicedoor" livable="false" measurable="false" optional="true">
      <label textStyle="labelStandard">PLAN 1871</label>
      <label textStyle="labelStandard">AC</label>
      <label textStyle="labelStandard">2-CAR GARAGE</label>
      <label textStyle="labelStandard">COVD PORCH</label>
      <label textStyle="labelStandard">COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD PATIO</label>
      <label textStyle="labelStandard">EXT. COVD. PORCH</label>
      <label textStyle="labelStandard">EXT. GARAGE</label>
      <label textStyle="labelStandard">SERVICE DOOR</label>
      <version id="0" default="false">
        <line>
          <point x="3.2501" y="16.7500" />
          <point x="3.2501" y="20.2500" />
        </line>
        <line>
          <point x="0.2500" y="20.2500" />
          <point x="0.2500" y="16.7500" />
        </line>
        <line>
          <point x="3.2501" y="20.2500" />
          <point x="0.2501" y="20.2500" />
        </line>
      </version>
    </region>
  </regions>
</plan>

DECLARE @PlanID SmallInt
SET @PlanID=1871
SELECT     @PlanID as '@id', garage AS '@garage',
      (SELECT fpDetails.regionId as '@id', fpDetails.tag as '@tag', fpDetails.livable as '@livable', fpDetails.measurable as '@measurable', fpDetails.optional as '@optional',
            (SELECT  1 as Tag,
        0 as Parent,
        planTitle  as [label!1!!CDATA],
        textStyle  as [label!1!textStyle]
            FROM    dbo.fpDetails 
            WHERE   planId=@PlanID
            FOR XML EXPLICIT, TYPE),
            (SELECT fpDetails.versionid as '@id', fpDetails.defaultOpt as '@default',
                  (SELECT '',
                        (SELECT fpLines.startX as '@x', fpLines.startY as '@y'
                        FOR XML PATH('point'), TYPE),
                        (SELECT fpLines.endX as '@x', fpLines.endY as '@y'
                        FOR XML PATH('point'), TYPE)
                  FROM fpLines 
                  WHERE (fpDetails.PlanID = fpLines.PlanID) AND (fpDetails.tag = fpLines.tag)
                  FOR XML PATH('line'), TYPE)
            FOR XML PATH('version'), TYPE)
      FROM fpDetails 
      WHERE fpDetails.PlanID = @PlanID
      FOR XML PATH('region'), root ('regions'), TYPE)
FROM dbo.parcel_plans WHERE planNumber = @PlanID FOR XML PATH ('plan'), TYPE

Open in new window

rolwingrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Wow, that is involved...

I am not so sure it does have to be quite so heavy...

Could you show a simple select statement of the columns you want, (ie joining fpdetails to fplines) and the required output ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rolwingrAuthor Commented:
I actually got it worked out.  I needed another parameter in the WHERE clause to get it to work:

(SELECT  1 as Tag,
        0 as Parent,
        '<![CDATA[' + planTitle + ']]>'  as [label!1!!CDATA],
        textStyle  as [label!1!textStyle]
            FROM    dbo.fpDetails fpd2
            WHERE   planId=@PlanID and fpDetails.regionId=fpd2.regionId

Anybody know if you can use for xml path to return the < and > 
SQL makes them url encoded and returns < and >
0
rolwingrAuthor Commented:
I was able to get ths resolved by adding in another parameter to the WHERE clause.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.