2

one with namespace prefix with 'emp'

<?xml version="1.0" encoding="UTF-8"?>
<emp:Employees xmlns:emp="http://www.xyz.com">
    <emp:EmployeeDetails>
        <emp:EmployeeCompanyDetails>
            <emp:CompanyName>XYZ</emp:CompanyName>
            <emp:Desgination>Engineer</emp:Desgination>
            <emp:YearOfExp>8</emp:YearOfExp>
            <emp:Department>HR</emp:Department>
            <emp:ProjectDetails>
                <emp:ProjectName>ABC</emp:ProjectName>
                <emp:Client>ZZZ</emp:Client>
                <emp:Manager>MMMM</emp:Manager>
            </emp:ProjectDetails>
        </emp:EmployeeCompanyDetails>
    </emp:EmployeeDetails>
</emp:Employees>

and the other without namespace prefix.

<emp:Employees xmlns:emp="http://www.xyz.com">
    <EmployeeDetails>
        <EmployeeCompanyDetails>
            <CompanyName>XYZ</CompanyName>
            <Desgination>Engineer</Desgination>
            <YearOfExp>8</YearOfExp>
            <Department>HR</Department>
            <ProjectDetails>
                <ProjectName>ABC</ProjectName>
                <Client>ZZZ</Client>
                <Manager>MMMM</Manager>
            </ProjectDetails>
        </EmployeeCompanyDetails>
    </EmployeeDetails>
</emp:Employees>

I have written xslt below to extract only <ProjectDetails> that has no namespace prefix defined.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:emp="http://www.xyz.com" version="1.0">
    <xsl:template match="/">
        <emp:ProjectDetails>        
            <xsl:copy-of select="*//ProjectDetails/*" /> 
            <xsl:copy>
                <!--xsl:apply-templates /-->
            </xsl:copy>
        </emp:ProjectDetails>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="ns:{local-name()}">
            <xsl:apply-templates select="@*|node()" />
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

Above xslt will work if input xml comes without namespace prefix. Suppose input xml comes with namespace prefix emp:, then i need to change my xslt code to <xsl:copy-of select="*//emp:ProjectDetails/*" /> .

My question : Is there anyway we can write a common xslt to extract portion of xml.

1
  • Good question, +1. See my answer for a very short and complete solution. :) Commented Jan 4, 2011 at 14:35

2 Answers 2

4

You can use:

<xsl:copy-of select="//*[local-name() = 'ProjectDetails']"/>
Sign up to request clarification or add additional context in comments.

Comments

3

Well with XSLT 2.0 you can use a wildcard select="*//*:ProjectDetails/*". And with XSLT 1.0 nothing prevents you from using select="*//emp:ProjectDetails/* | *//ProjectDetails/*" or select="*//*[local-name() = 'ProjectDetails']/*". That approach allows you to handle both kind of input documents with one select attribute in your stylesheet.

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.