JohnX
JohnX

Reputation: 245

Can't get debug output correctly using XSLT/XPath 2.0

I have an XML like this:

<?xml version="1.0" encoding="UTF-8"?>

<Section>
    <Chapter>
        <Cell colname="1">
            <Value>A</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>AAA</MyValue>
            <MyValue>BBB</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Honda</MyCar>
        </Cell>
    </Chapter>
    <Chapter>
        <Cell colname="1">
            <Value>C</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>CCC</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Toyota</MyCar>
        </Cell>
    </Chapter>
</Section>

I have an XSLT like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="Section/Chapter"/>
    </xsl:template>

    <xsl:template match="Chapter">
        <xsl:apply-templates select="./Cell/MyValue"/>
    </xsl:template>

    <xsl:template match="MyValue">
        <xsl:message>
            <xsl:value-of select="../../Cell/@colname"/>
            <xsl:value-of select="../../Cell[@colname='1']/Value"/>
            <xsl:value-of select="."/>
            <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
        </xsl:message>
    </xsl:template>

    <xsl:template match="text()" />

</xsl:stylesheet>

Problem is the debug output shows up as 1 2 3 A AAA Honda 1 2 3 A BBB Honda 1 2 3 C CCC Toyota. I'd like to get something like 1 A 2 AAA 3 Honda 1 A 2 BBB 3 Honda 1 C 2 CCC 3 Toyota. Basically getting the value of the attribute colname correctly.

So several questions:

  1. What did I do wrong ?.
  2. There is a sequence 1 2 3 generated why ?.

TIA,

John

Upvotes: 2

Views: 222

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

So several questions:

  1. What did I do wrong ?.

You didn't specify exactly the necessary expressions.

  1. There is a sequence 1 2 3 generated why ?.

Because ../../Cell/@colname selects the colname attribute of every Cell child of the current node's grandparent Chapter.

Solution:

Use:

<xsl:template match="MyValue">
    <xsl:message>
        <xsl:value-of select="../../Cell[1]/@colname"/>
        <xsl:value-of select="../../Cell[@colname='1']/Value"/>
        <xsl:value-of select="../@colname"/>
        <xsl:value-of select="."/>
        <xsl:value-of select="../../Cell[3]/@colname"/>
        <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
    </xsl:message>
</xsl:template>

The debug output is exactly the wanted:

1A2AAA3Honda
1A2BBB3Honda
1C2CCC3Toyota

You also can use a slight modification of the answer to your previous question:

<xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>

     <xsl:template match="Chapter">
      <xsl:apply-templates select="Cell[1]"/>
     </xsl:template>

     <xsl:template match="Cell">
      <xsl:param name="pText" as="xs:string*"/>

      <xsl:apply-templates select="*[1]">
       <xsl:with-param name="pText" select="$pText"/>
      </xsl:apply-templates>
     </xsl:template>

     <xsl:template match="Cell/*">
      <xsl:param name="pText" as="xs:string*"/>

      <xsl:variable name="vText" as="xs:string*"
       select="$pText, ../@colname, string(.)"/>

      <xsl:sequence select=
       "$vText
          [not(current()/../following-sibling::Cell)]"/>
      <xsl:apply-templates select="../following-sibling::Cell[1]">
        <xsl:with-param name="pText" select="$vText"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="following-sibling::*">
        <xsl:with-param name="pText" select="$pText"/>
      </xsl:apply-templates>
     </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<Section>
    <Chapter>
        <Cell colname="1">
            <Value>A</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>AAA</MyValue>
            <MyValue>BBB</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Honda</MyCar>
        </Cell>
    </Chapter>
    <Chapter>
        <Cell colname="1">
            <Value>C</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>CCC</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Toyota</MyCar>
        </Cell>
    </Chapter>
</Section>

the wanted, correct result is produced:

1 A 2 AAA 3 Honda 1 A 2 BBB 3 Honda 1 C 2 CCC 3 Toyota

Upvotes: 2

Kevan
Kevan

Reputation: 891

The following produces the desired result:

  1. You were getting 1 2 3 because ../../Cell/@colname matches all three cells in the current chapter. That's why I changed it to Cell[1]
  2. I am not sure how 'hard coded' the colnames are supposed to be as using the colname 3, for example, to look up the colname's value of 3 seems rather strange. That's why I simply output it as text

?xml version="1.0" encoding="UTF-8"?>

<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

<xsl:template match="/">
    <xsl:apply-templates select="Section/Chapter"/>
</xsl:template>

<xsl:template match="Chapter">
    <xsl:apply-templates select="./Cell/MyValue"/>
</xsl:template>

<xsl:template match="MyValue">
    <xsl:message>
        <xsl:value-of select="../../Cell[1]/@colname"/>
        <xsl:value-of select="../../Cell[@colname='1']/Value"/>
        <xsl:value-of select="../@colname"/>
        <xsl:value-of select="."/>
        <xsl:text>3</xsl:text>
        <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
    </xsl:message>
</xsl:template>

<xsl:template match="text()" />

</xsl:stylesheet>

Upvotes: 0

Related Questions