5

I have the following xml file:

<xfa:data>
  <form1>
    <Page1>
    <Page2>
    <contractInfo> ... </contractInfo>
    <paymentInfo> ... </paymentInfo>
  </form1>
  <commercialType> .... </commercialType>
  <userList> ... </userList>
  <officesList> ... </officesList>
  <commercialType> .... </commercialType>
  <userList> ... </userList>
  <officesList> ... </officesList>
  <commercialType> .... </commercialType>
  <userList> ... </userList>
  <officesList> ... </officesList>
</xfa:data>

I want to remove every ocurrence of the commercialType, userList and officesList nodes, so my output would be :

<xfa:data>
  <form1>
    <Page1>
    <Page2>
    <contractInfo> ... </contractInfo>
    <paymentInfo> ... </paymentInfo>
  </form1>
</xfa:data>

How could I do that using XSLT?

Thank you

1
  • Good Question (+1). See my answer for a complete solution using and overriding the identity rule. :) Commented Apr 14, 2010 at 23:21

2 Answers 2

20

This transformation produces the desired results:

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

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

 <xsl:template match="commercialType|userList|officesList"/>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

8 Comments

@Rohit, What is "it" and what "does not work"? Are you sure that you have the same source XML document as in this question? Are you sure that you execute the same transformation as the one in this answer? Are you sure you are using a compliant, non-buggy XSLT processor?
@DimitreNovatchev I do not have same xml or xslt but similar. I am using Visual Studio 2012. I found that using default namespace does not work. Thanks, works now.
@Rhyous, 'xmlns="..."' is declaration of a default namespace. xmlns:somePreffix = "..." is a declaration of non-default namespace. Your problem is a FAQ. Search for "default namespace and XPath" and you will find many good explanations. And yes, this answer will need to be modified in the case when namespaces are used in the source XML document. However, the current question doesn't have such source XML document. Therefore, once again, do read other questions and there are many on the topic of namespaces.
@zygimantus, Maybe this answer will help: stackoverflow.com/a/10712711/36305
@zygimantus, Yes, you need to do searching or more systematic learning. Just have <xsl:applu-templates/> inside the matching template.
|
-1

Identity transformation plus <xsl:template match="commercialType" priority="10"/>.

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.