We help IT Professionals succeed at work.
Get Started

Apply multiple xsl match template on a xml

enthuguy
enthuguy asked
on
1,949 Views
Last Modified: 2012-08-30
Hi friends, I'm new to XML/XSLT...can you help as usual please

Scenario is, I have several xml files where I need to remove certain sections based on the value I specify. Basically to appply multiple xslt templates on a XML document.

please check the attached sample xml and below xslt document (I couldnt attach it)
sample.xml: contains 4 sections.
testone.xslt: Has got one template to remove "First" section. this works fine.
testtwo.xslt: Has got two templates to remove "First" and "Third" section. this isnt working as expected.

I was trying to apply multiple templates on a single document in one go.

thanks in advance.

testone.xslt
============
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:ns1="urn:/crmondemand/xml/fieldmanagement/data"
exclude-result-prefixes="ns1">
 <xsl:param name="testParam"/>
<xsl:output method="xml" indent="yes" />
      <xsl:template match="@*|node()" >
          <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
          </xsl:copy>
      </xsl:template>
      
      <xsl:template match="ns1:ListOfFields/ns1:Field">
        <xsl:choose>
            <xsl:when test="ns1:ListOfFieldTranslations/ns1:FieldTranslation[starts-with(ns1:DisplayName, 'First')]">
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>  
      </xsl:template>
</xsl:stylesheet>



testtwo.xslt
============
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:ns1="urn:/crmondemand/xml/fieldmanagement/data"
exclude-result-prefixes="ns1">
 <xsl:param name="testParam"/>
<xsl:output method="xml" indent="yes" />
      <xsl:template match="@*|node()" >
          <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
          </xsl:copy>
      </xsl:template>
      
      <xsl:template match="ns1:ListOfFields/ns1:Field">
        <xsl:choose>
            <xsl:when test="ns1:ListOfFieldTranslations/ns1:FieldTranslation[starts-with(ns1:DisplayName, 'First')]">
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>  
      </xsl:template>
        
        <xsl:template match="ns1:ListOfFields/ns1:Field">
        <xsl:choose>
            <xsl:when test="ns1:ListOfFieldTranslations/ns1:FieldTranslation[starts-with(ns1:DisplayName, 'Third')]">
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>  
      </xsl:template>
</xsl:stylesheet>
sample.xml
Comment
Watch Question
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE