0

I wanted to remove all the entries from below xml which has "tig:" in it. I tried the below transformation xsl to convert it and it does not work.

 <ContactInfo>
    <PersonName>
        <FormattedName>My Name</FormattedName>
        <GivenName>Test first Name</GivenName>
        <FamilyName>Test Last Name</FamilyName>
    </PersonName>
       </ContactInfo>
    <tig:TestArea>
        <tig:UserArea>
            <tig:ParseTime>9000</tig:ParseTime>
        </tig:UserArea>
        <tig:Country>
            <tig:Language>en</tig:Language>
        <tig:Country>CAN</tig:Country>
        </tig:Country>
    </tig : TestArea>

I tried the below transformation xsl to convert it and it does not work. tig: is a namespace as well and I want to remove all the elements related to that namespace.

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

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

        <xsl:template match="tig:">
            <xsl:apply-templates/>
        </xsl:template>

    </xsl:stylesheet>

3 Answers 3

1

Instead of

 <xsl:template match="tig:">
        <xsl:apply-templates/>
    </xsl:template>

use

<xsl:template match="tig:"/>
1
  • This cannot work, because (a) "tig:" is not a valid match pattern and (2) the prefix tig is not bound to a namespace. Jan 20, 2015 at 9:06
1

Given a well-formed input, such as:

<root>
    <ContactInfo>
        <PersonName>
            <FormattedName>My Name</FormattedName>
            <GivenName>Test first Name</GivenName>
            <FamilyName>Test Last Name</FamilyName>
        </PersonName>
    </ContactInfo>
    <tig:TestArea xmlns:tig="http://www.example.com/tig">
            <tig:UserArea>
                <tig:ParseTime>9000</tig:ParseTime>
            </tig:UserArea>
            <tig:Language>en</tig:Language>
            <tig:Country>CAN</tig:Country>
    </tig:TestArea>
</root>

the following stylesheet:

XSLT 1.0

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

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

<xsl:template match="tig:*" xmlns:tig="http://www.example.com/tig"/>

</xsl:stylesheet>

will remove all elements in the namespace bound to the tig prefix (along with their descendants), resulting in:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <ContactInfo>
      <PersonName>
         <FormattedName>My Name</FormattedName>
         <GivenName>Test first Name</GivenName>
         <FamilyName>Test Last Name</FamilyName>
      </PersonName>
   </ContactInfo>
</root>
0

The first thing to say is that your XML is lacking the namespace declaration for the "tig" prefix. It is also lacking a single root element, so I am assuming there is a root element in your actual XML where the namespace is declared

<Data xmlns:tig="http://tig">
   <ContactInfo>

You would also need to declare the namespace in your XSLT

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:tig="http://tig">

But in answer to your question, the reason it does not work (more likely, you are getting an error message), is that the syntax should be this

<xsl:template match="tig:*">
    <xsl:apply-templates/>
</xsl:template>

Note that this removes (or rather, doesn't output) the element, but it does continue to process its children, which will ultimately result in the text nodes (such as 'en' and 'CAN') being output. To stop this, you may need to add the following template too

<xsl:template match="tig:*/text()" />

Try this XSLT

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:tig="http://tig">

    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="tig:*">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="tig:*/text()" />
</xsl:stylesheet>

Note, you could replace the two tig templates with the following matching template

<xsl:template match="tig:*" />

However, if you had elements nested under a tig element which belonged to a different namespace (or were not in a namespace at all), these would be removed too.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.