Ihe Onwuka
Ihe Onwuka

Reputation: 477

Template match on the text value of a parent node

Here is some XML

<flowTypologies type="flowTypologies">
  <flowTypology type="flowTypology">
    <label type="string">Typology0</label>
    <value type="string">CAP</value>
  </flowTypology>
  <flowTypology type="flowTypology">
    <label type="string">Typology1</label>
    <value type="string"/>
  </flowTypology>
  <flowTypology type="flowTypology">
    <label type="string">Typology2</label>
    <value type="string"/>
  </flowTypology>
  <flowTypology type="flowTypology">
    <label type="string">Typology3</label>
    <value type="string"/>
  </flowTypology>
  <flowTypology type="flowTypology">
    <label type="string">Typology4</label>
    <value type="string">INT</value>
  </flowTypology>
  <flowTypology type="flowTypology">
    <label type="string">GlobalCashflowTypology</label>
    <value type="string"/>
  </flowTypology>
</flowTypologies>

Here is a stylesheet snippet to transform it.

<xsl:stylesheet
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
exclude-result-prefixes="xs" version="1.0">
   <xsl:strip-space elements="*"/>
   <xsl:template  match="flowTypologies[. = 'Typology0CAPTypology1Typology2Typology3Typology4INTGlobalCashFlowTypology']">
      <xsl:text>I should get here</xsl:text>
   </xsl:template>

</xsl:stylesheet>

So the hope was that to template match on the the text value of the top level node. The string in the template predicate should be the concatenation of the text in all the subordinate nodes under flowTypologies.

However it does not match.

This is an XSLT 1.0 stylesheet I have tried transforming it with Saxon 6.5 and Xalan.

I have done a value-of on flowTypologies and it does yield the value being tested for in the predicate, but rule never fires.

Upvotes: 1

Views: 439

Answers (2)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

Simply reduce the template match to:

<xsl:template  match="flowTypologies">
  <xsl:text>I should get here</xsl:text>
</xsl:template>

I cannot think of a reason to use a key here. Keys are really in the wrong place if they are defined for the outermost element, because there is only one of them. Also, you should never rely on the concatenation of strings to qualify a template match. For example, it is risky because whitespace is not handled in the same way by all implementations.

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 116959

However it does not match.

It does not match, because XML is case-sensitive: "GlobalCashflowTypology" is not the same thing as "GlobalCashFlowTypology".

That aside, I am not sure what you're trying to achieve here, but there's bound to be a better way than using a humongous string to match the wrapper?

Upvotes: 1

Related Questions