user3657957
user3657957

Reputation: 11

Can i have if statement inside a Template Match= in xslt

I want acheive 3 things.

First is rename the GroupArea to GroupBox Nodes.

Second is Put a tag after the GroupBox and GroupArea Node.

Third is to move all child GroupArea and GroupBox Nodes and its content to ParentNode of XML. To acheive this i am using 2 template match and this is messing up things.

I cannot upload an image to show this for now.

If i can put a If-else logic inside the first templateMatch then i should be fine. I think i cannot do that. But by using 2 of them, the line "xsl:apply-templates select="node()" in the second templateMatch is coping over the entire node including child GroupArea to a GroupArea instead of just renaming the GroupArea to GroupBox.

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

<xsl:template match="@*|node()">
<xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>


<!--First Template Match. To Move all the child GroupArea and GroupBox to Parent and inject a Blank Controls Tag-->
<xsl:template match="GroupBox|GroupArea">
<xsl:copy>
    <xsl:apply-templates select="@*[not(self::GroupBox | self::GroupArea )]" />
<GroupBox>
<Controls>
         <xsl:apply-templates select="node()[not(self::GroupBox | self::GroupArea )]" />  
    </Controls>
</GroupBox>
</xsl:copy>
<xsl:apply-templates select="GroupBox|GroupArea|GROUPAREA|GROUPBOX" />
</xsl:template>



<!--Second Template Match. To Convert all the GroupArea to GroupBox and then Inject the Blank Control Tags-->
<xsl:template match="GroupArea">
<GroupBox>
<xsl:apply-templates select="@*" /> 
<Controls>
    <xsl:apply-templates select="node()" />  
</Controls>  
</GroupBox>
</xsl:template>


</xsl:stylesheet>

Is there anyway to acheive the same. XML FILE Below

<MyControls>
<GroupBox Id="GroupBox1" Title="Public Event Details">
<Textbox HasPopup="False" Id="NameOfEvent" Title="Name of Event"> </Textbox>
<GroupBox Id="ctrl_0" Title="">
<CheckBox  Title="Public Property" AccessItem="0"></CheckBox>
<Textbox HasPopup="False" Id="Location" Title="Location or Route" ></Textbox>
</GroupBox>

<GroupArea Id="ctrl_1" Title="">
<Textbox  Id="_MATaxNumber" Title="MA Tax Number"  AccessItem="0" HintText=""></Textbox>
<Textbox HasPopup="False"  Id="AttExp" Title="Attes Expected" ></Textbox>
</GroupArea>

<GroupBox Id="ctrl_2" Title=""/>

<GroupBox Id="ctrl_3" Title="">
    <CheckBox  Id="CBPublicEventDetails_VendingFood" Title="Food"  AccessItem="0"></CheckBox>
    <CheckBox  Id="CBPublicEventDetails_VendingGoods" Title="Goods" AccessItem="0"></CheckBox>
    <Textbox HasPopup="False" Id="VendingVendors" Title="Total Vendors" ></Textbox>
</GroupBox>

<GroupArea Id="ctrl_4" Title="">
<CheckBox Id="CBPuEntLiveMusic" Title="Live Music"  AccessItem="0"></CheckBox>
<CheckBox Id="CBPubliEntStage" Title="Stage" AccessItem="0"></CheckBox>
</GroupArea>

<GroupBox Id="ctrl_5" Title="">
    <CheckBox  Id="CBPublicEventDetails_GamesGames" Title="Games" AccessItem="0"></CheckBox>
    <CheckBox  Id="CBPublicEventDetails_GamesOther" Title="Other"  AccessItem="0"></CheckBox>
</GroupBox>


<GroupBox Id="ctrl_6" Title="">
<Textbox HasPopup="False"  Id="SecurityHow" Title="How will they be identified?"/>
<Textbox HasPopup="False" Id="SecurityNumber" Title="Security Personnel #"/>
</GroupBox>

<GroupBox Id="ctrl_19" Title="">
<CheckBox Id="cb_pubeventdetails_emswalk" Title="Run/Walk" AccessItem="0"></CheckBox>
<CheckBox  Id="cb_pubeventdetails_emsboat" Title="Boating/Swim" AccessItem="0"></CheckBox>



<GroupArea id="gaAdjustmentResults">
    <Grid id="gdAdjustmentResults" >
        <Columns>               
                            <GridColumn ID="gc1"  DataType="Int"></GridColumn>      
        </Columns>
    </Grid>

    <GroupArea id="gaAdjustmentNew" >
        <Grid id="gdAdjustmentResults" >
        <Columns>
            <GridColumn ID="gcAdjustment"  DataType="Int"></GridColumn> 
        </Columns>
        </Grid>
    </GroupArea>
</GroupArea>
</GroupBox>
</GroupBox>
</MyControls>

WHAT I AM GETTING AS OUTPUT WITH THIS XSL. SHOWING JUST THE SECTION WITH ISSUE. AT THE BOTTOM I HAVE ATTACHED THE COMPLETE TRANSFORMED XML.

WHILE USING THE XML AND XSL YOU WILL SEE BELOW RESULT SEE I HAVE A INNER GROUPBOX STILL FOUND. IF YOU REMORE THE SECOND TEMPLATEMATCH YOU WILL NOT FIND THE INNER GORUPAREA BUT IT WILL BE A GROUPAREA AND NOT A GROUPBOX

<GroupBox id="gaAdjustmentResults">
<Controls>
  <Grid id="gdAdjustmentResults">
    <Columns>
      <Label ID="gc1" DataType="Int">
      </Label>
    </Columns>
  </Grid>
  <GroupBox id="gaAdjustmentNew">
    <Controls>
      <Grid id="gdAdjustmentResults">
        <Columns>
          <Label ID="gcAdjustment" DataType="Int">
          </Label>
        </Columns>
      </Grid>
    </Controls>
  </GroupBox>
</Controls>

FURTHER IF YOU SEE THE TRANSFORMATION, IS THERE ANYWAY I CAN MOVE THE 2 GRID NODES AND ADD IT TO THE BOTTOM OF THE FILE INSIDE THE OUTER CONTROLS TAG.

Expected Output

<MyControls>
<GroupBox Id="GroupBox1" Title="Public Event Details">
<Controls>
  <Textbox HasPopup="False" Id="NameOfEvent" Title="Name of Event">
  </Textbox>
</Controls>
</GroupBox>
<GroupBox Id="ctrl_0" Title="">
<Controls>
  <CheckBox Title="Public Property" AccessItem="0">
  </CheckBox>
  <Textbox HasPopup="False" Id="Location" Title="Location or Route">
  </Textbox>
</Controls>
</GroupBox>
<GroupBox Id="ctrl_1" Title="">
<Controls>
  <Textbox Id="_MATaxNumber" Title="MA Tax Number" AccessItem="0" HintText="">
  </Textbox>
  <Textbox HasPopup="False" Id="AttExp" Title="Attes Expected">
  </Textbox>
</Controls>
</GroupBox>
<GroupBox Id="ctrl_2" Title="">
<Controls>
</Controls>
</GroupBox>
<GroupBox Id="ctrl_3" Title="">
<Controls>
  <CheckBox Id="CBPublicEventDetails_VendingFood" Title="Food" AccessItem="0">
  </CheckBox>
  <CheckBox Id="CBPublicEventDetails_VendingGoods" Title="Goods" AccessItem="0">
  </CheckBox>
  <Textbox HasPopup="False" Id="VendingVendors" Title="Total Vendors">
  </Textbox>
</Controls>
</GroupBox>
...........
...........

<GroupBox id="gaAdjustmentResults">
<Controls>
  <Grid id="gdAdjustmentResults">
    <Columns>
      <Label ID="gc1" DataType="Int">
      </Label>
    </Columns>
  </Grid>
</Controls>
</GroupBox>

<GroupBox id="gaAdjustmentNew">
    <Controls>
      <Grid id="gdAdjustmentResults">
        <Columns>
          <Label ID="gcAdjustment" DataType="Int">
          </Label>
        </Columns>
      </Grid>
    </Controls>
  </GroupBox>
</GroupBox>
</MyControls>

Upvotes: 1

Views: 1981

Answers (1)

helderdarocha
helderdarocha

Reputation: 23637

Based on what you posted, I came up with some examples which may help you find a solution. I'm assuming some things since you didn't post your input source.

All you have to do to rename GroupArea to GroupBox (wherever they may be in your source), and not process children which are GroupArea or GroupBox is:

<xsl:template match="GroupArea">
    <GroupBox>
        <xsl:apply-templates select="@*|node()[not(self::GroupBox | self::GroupArea )]" />
    </GroupBox>
</xsl:template>

Assuming you have an identity transform somewhere:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

the descendants and attributes will be copied to the result tree.

You also want an element after the <GroupBox> nodes. I didn't understand what you want inside it, so I left it empty:

<xsl:template match="GroupArea | GroupBox">
    <GroupBox>
        <xsl:apply-templates select="@*|node()[not(self::GroupBox | self::GroupArea )]" />
    </GroupBox>
    <Controls></Controls>
</xsl:template>

Now, to make all your <GroupBox> nodes children of the document element, you just need to match a template in the document element and select your GroupArea | GroupBox nodes from there:

<xsl:template match="RootElement">
    <xsl:apply-templates select="//GroupBox | //GroupArea" />
    ...
</xsl:template>

Try this stylesheet. If the structure of your source is not too different from what I imagined, it might work at least as a starting point:

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

    <xsl:template match="RootElement">
        <xsl:apply-templates select="//GroupBox | //GroupArea" />
    </xsl:template>

    <xsl:template match="GroupArea | GroupBox">
        <GroupBox>
            <xsl:apply-templates select="@*|node()[not(self::GroupBox | self::GroupArea )]" />
        </GroupBox>
        <Controls></Controls>
        <xsl:apply-templates select="node()[self::GroupBox | self::GroupArea]" />
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions