0

I have an xml and want to filter out sections that I do not need. Here is the xml & respective expected out put,

    <CD>
  <RT></RT>
  <author></author>
  <component>
    <structurebody>
      <component>
        <section>
          <templateid root = "1" />
        </section>
        </component>
        <component>
          <section>
            <templateid root = "2" />
          </section>
        </component>
        <component>
          <section>
            <templateid root = "3" />
          </section>
          </component>
        </structurebody>
    </component>
  </CD>

I want the output to contain sections with template id's 1 and 3 not 2. I am trying the following xslt but it does not work. It outputs everything. Not sure what is wrong

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

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

  <xsl:template match="/component/structuredBody/component/section/templateId[not(@root = '1' or @root = '3')]" />
</xsl:stylesheet>

Can anyone let me know where am I making a mistake? Appreciate your help.

1 Answer 1

1

Try this...

<xsl:template match="/CD/component/structuredBody/component/section[templateId[not(@root = '1' or @root = '3')]]" />

Note this initial /CD in the xpath. Also, note this matches section not templateId (assuming you wish to remove the section element, not just the child template.

Actually, you can just simplify it to this. Specifying the whole hierarchy is not strictly necessary here.

<xsl:template match="section[templateId[not(@root = '1' or @root = '3')]]" />
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.