2

Source XML:

<MP>
  <Name>pol</Name>
  <PRules>
  <PRule order="1" name="r1">
   <Conditions>
    <Condition eleName="eth" value="05">05</Condition>
    <Condition eleName="dest" value="32">32</Condition>
   </Conditions>
  </PRule>
  <PRule order="2" name="r2">
   <Conditions>
    <Condition eleName="eth" value="04">04</Condition>
   </Conditions>
   <Actions>
    <Action name="xyz"/>
   </Actions>
  </PRule>
 </PRules>
</MP>

If a Condition node with attribute eleName="eth" has to be deleted. After deletion of Condition node if Conditions is empty, complete PRule node also has to be removed.

I have applied the following XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" omit-xml-declaration="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template name="attributeTemplate" match="Condition[@elementName='eth']"/>

 <xsl:template match="PRule[descendant::Conditions[not(@*)]]"/>
</xsl:stylesheet>

But the result is coming like this:

<MP>
 <Name>pol</Name>
 </PRules>
</MP>

What change I have to make to transform the XML as

<MP>
 <Name>pol</Name>
 <PRules>
  <PRule name="r1" order="1">
   <Conditions>
    <Condition eleName="dest" value="32">32</Condition>
   </Conditions>
  </PRule>
 </PRules>
</MP>

What went wrong in xsl file, I don't understand. Basically I wanted remove the parent PRule node if Conditions is empty.

3
  • There is some contradiction here in your apparent rules. Your templates and heading suggest (but you have not made it clear in your narrative), that if an element has no attributes and no child elements, then it should be removed. But your statement about PRule being eliminated contradicts this, as PRule has attributes. Commented Jul 18, 2012 at 12:01
  • This will be an easy stylesheet to make once, you have clarified the rules. But at the moment, the rules are not clear. Commented Jul 18, 2012 at 12:02
  • @SeanB.Durkin: The requirements are stated well. Commented Jul 18, 2012 at 13:00

1 Answer 1

4

This transformation:

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

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

 <xsl:template match="PRule[not(*/Condition[not(@eleName='eth')])]"/>

 <xsl:template match="Condition[@eleName = 'eth']"/>
</xsl:stylesheet>

when applied on the provided XML document:

<MP>
    <Name>pol</Name>
    <PRules>
        <PRule order="1" name="r1">
            <Conditions>
                <Condition eleName="eth" value="05">05</Condition>
                <Condition eleName="dest" value="32">32</Condition>
            </Conditions>
        </PRule>
        <PRule order="2" name="r2">
            <Conditions>
                <Condition eleName="eth" value="04">04</Condition>
            </Conditions>
            <Actions>
                <Action name="xyz"/>
            </Actions>
        </PRule>
    </PRules>
</MP>

produces the wanted, correct result:

<MP>
   <Name>pol</Name>
   <PRules>
      <PRule order="1" name="r1">
         <Conditions>
            <Condition eleName="dest" value="32">32</Condition>
         </Conditions>
      </PRule>
   </PRules>
</MP>

Explanation:

Proper use of the identity rule and the double negation law.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.