Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt mapping

Tags:

xml

xslt

I have an xml file with the following values -

<InspectionChecklist>
    <InspectionChecklistItem>
        <ChecklistItemDescription>Frame damaged</ChecklistItemDescription>
        <ChecklistItemValue>1</ChecklistItemValue>
    </InspectionChecklistItem>
    <InspectionChecklistItem>
        <ChecklistItemDescription>Smokers Flag</ChecklistItemDescription>
        <ChecklistItemValue>1</ChecklistItemValue>
    </InspectionChecklistItem>
</InspectionChecklist>

And I want the output to look like -

<FrameDamage>Y</FrameDamage>
<SmokerFlag>Y</SmokerFlag>

So in the source xml, it is possible that I won't have any ChecklistItemDescription, or other check list item descriptions, such as -

Example 1 -

Source

<InspectionChecklist></InspectionChecklist>

I want the output to look like

<FrameDamage>N</FrameDamage>
<SmokerFlag>N</SmokerFlag>

Example 2-

Source

<InspectionChecklist>
    <InspectionChecklistItem>
        <ChecklistItemDescription>Airbag Light</ChecklistItemDescription> 
        <ChecklistItemValue>1</ChecklistItemValue> 
    </InspectionChecklistItem>
    <InspectionChecklistItem>
        <ChecklistItemDescription>Smokers Flag</ChecklistItemDescription> 
        <ChecklistItemValue>1</ChecklistItemValue> 
    </InspectionChecklistItem>
</InspectionChecklist>

The output should look like -

<FrameDamage>N</FrameDamage>
<SmokerFlag>Y</SmokerFlag>

I have done a few ways and can get individual ones working. But I can't get them to work for every possible case.

Any help would be appreciated.

like image 514
Patricia Avatar asked May 21 '26 12:05

Patricia


1 Answers

This XPath expression

/InspectionChecklist
   /InspectionChecklistItem
      /ChecklistItemDescription = 'Frame damaged' 

It results in boolean value: there is a ChecklistItemDescription with "Frame dameged" string value (true) or not (false).

We can convert this to Y or N by this expression (between others):

substring('NY', $condition + 1, 1)

I leave you the composition as a exercise to you.