Xslt mapping

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 result to look like

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

So, in the original xml, it is possible that I will not have any ChecklistItemDescription or other descriptions of the elements of the checklist, such as -

Example 1 -

Source

<InspectionChecklist></InspectionChecklist>

I want the result 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 result should look like this:

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

I have done several ways and can make individual ones work. But I can not get them to work for every possible occasion.

Any help would be appreciated.

+3
source share
2 answers

This is an XPath expression

/InspectionChecklist
   /InspectionChecklistItem
      /ChecklistItemDescription = 'Frame damaged' 

This leads to a boolean value: there is ChecklistItemDescriptionwith the string value "Frame dameged" (true) or not (false).

Y N ( ):

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

.

+1

:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:output>
   <frame>
    <FrameDamage>N</FrameDamage>
    <FrameDamage>Y</FrameDamage>
   </frame>
   <smoker>
    <SmokerFlag>N</SmokerFlag>
    <SmokerFlag>Y</SmokerFlag>
   </smoker>
 </my:output>

 <xsl:variable name="vFrameOutputs" select="document('')/*/my:output/frame/*"/>
 <xsl:variable name="vSmokerOutputs" select="document('')/*/my:output/smoker/*"/>
 <xsl:variable name="vDoc" select="/"/>

    <xsl:template match="/">
     <xsl:copy-of select=
       "$vFrameOutputs[($vDoc/*/*/ChecklistItemDescription='Frame damaged')+1]"/>
     <xsl:copy-of select=
       "$vSmokerOutputs[($vDoc/*/*/ChecklistItemDescription='Smokers Flag')+1]"/>
    </xsl:template>
</xsl:stylesheet>

XML-:

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

, :

<FrameDamage 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my">Y</FrameDamage>
<SmokerFlag 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my">N</SmokerFlag>

. , my:output. document() , .

+1

Source: https://habr.com/ru/post/1787524/


All Articles