1

I have xslt2.0 transformation issue with namespace, please help to solve this issue

Few XML have namespace attribute, Few XML don't have Namespace attribute.

File 1:

<root id="BC" xmlns="http://www.example.com/ptktims"><child  xmlns="http://www.example.com/times"></child></root>

File 2:

<root id="BC"><child></child></root>

The First file working fine, when i call namespace <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.example.com/">, but second file doe not working.

The Second file working fine, when i not call namespace <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">, but first file does not working.

Please advice, how to transform both XML form Single XSLT?

3
  • Your observation is correct - some XML documents have namespaces, others do't. But what is your question? Commented Mar 17, 2014 at 8:15
  • i need to transform both XML from Single XSLT. Commented Mar 17, 2014 at 8:42
  • The answer depends on the stylesheet you'd like to apply. Please 1 share your entire XSLT code, 2 say exactly how many XML documents there are and which namespaces they have, and 3 explain what your goal is: what is it you want to achieve with the transformation? Commented Mar 17, 2014 at 8:47

1 Answer 1

1

You have two options, use a wild card for the namespace prefix:

<xsl:template match="*:root">
  <xsl:value-of select="*:child"/>
</xsl:template>

or write matches for the different namespaces e.g.

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ptktims="http://www.example.com/ptktims"
  xmlns:times="http://www.example.com/times" version="2.0">

    <xsl:template match="root">
      <xsl:value-of select="child"/>
    </xsl:template>

    <xsl:template match="ptktims:root">
      <xsl:value-of select="times:child"/>
    </xsl:template>

A third option is to chain transformations or transformation steps where the first step normalizes the namespaces (either strips all or puts the ones you want in place) and the second step then only needs to work with templates for the normalized input format.

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.