Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top