PoweredByPorkers
PoweredByPorkers

Reputation: 243

XSLT combine multiple nodes into single node

<RowSet>
 <Row>
  <Location_Long_Desc>Sydney Office</Location_Long_Desc>
  <Location_Code>SYDNEY</Location_Code>
  <Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
  <Daypart_Code>PEANIG</Daypart_Code>
  <W_20050703_Dlr>30849.3</W_20050703_Dlr>
  <W_20050703_Spots>9</W_20050703_Spots>
  <W_20050710_Dlr>16.35</W_20050710_Dlr>
  <W_20050710_Spots>19</W_20050710_Spots>
 </Row>
</RowSet>

So, I have this XML now what I need to convert the W_ nodes into a new single node. Using this XSL

<?xml version="1.0"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*">
    <xsl:variable name="tmp" select="local-name()"/>
    <xsl:choose>
      <xsl:when test="starts-with($tmp, 'W_') and ends-with($tmp, '_Dlr')">
    <xsl:if test="text() != ''">
          <xsl:element name="Expenditure">
            <xsl:element name="Period">
              <xsl:value-of select="substring($tmp,3,8)"/>
            </xsl:element>
            <xsl:element name="Value">
              <xsl:apply-templates select="node()"/>
            </xsl:element>
            <xsl:element name="Spots">
              <xsl:apply-templates select="//RowSet/Row/W_20050703_Spots/text()"/>
            </xsl:element>
          </xsl:element>
    </xsl:if>
    </xsl:when>
    <xsl:otherwise>
        <xsl:element name="{$tmp}">
          <xsl:apply-templates select="node()"/>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

I can almost get there but I have a couple of issues.

  1. I need to combine the W_????????Dlr and W????????_Spots into a individual nodes but
  2. I can't work out how to use a variable in an xpath statement, or maybe I'm miles off where I should be.

Again, I'm still getting to grips with it all, so please be gentle ;-)

TIA

EDIT: 02/12/2010 12:00

Ok,

Just a little further question, depending on a database level switch (which I forgot all about), the Spots node may or may not exist.

So I still need to output though it shouldn't to the following-sibling call where the following-sibling isn't a valid _spots node.

Example:

<RowSet>
 <Row>
  <Location_Long_Desc>Sydney Office</Location_Long_Desc>
  <Location_Code>SYDNEY</Location_Code>
  <Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
  <Daypart_Code>PEANIG</Daypart_Code>
  <W_20050703_Dlr>30849.3</W_20050703_Dlr>
  <W_20050710_Dlr>16.35</W_20050710_Dlr>
 </Row>
</RowSet>

Just so you know, I'm calling all this via an Oracle package

-- get the query context;
v_qryctx := dbms_xmlgen.newcontext(in_sql_query);

dbms_xmlgen.setnullhandling(v_qryctx, 2);
dbms_xmlgen.setrowsettag(v_qryctx, 'RowSet');
dbms_xmlgen.setrowtag(v_qryctx, 'Row');

IF in_export_type = cnst_export_booking
THEN
    dbms_xmlgen.setxslt(v_qryctx, v_booking_export_xsl);

ELSIF in_export_type = cnst_export_expenditure
THEN
    dbms_xmlgen.setxslt(v_qryctx, v_expenditure_export_xsl);

END IF;

Upvotes: 4

Views: 6613

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

This transformation:

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

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match=
 "*[starts-with(name(),'W_')
  and
   substring-after(substring-after(name(),'_'),'_')='Dlr'
  and
   text()
   ]">
  <Expenditure>
    <Period>
      <xsl:value-of select="substring(name(),3,8)"/>
    </Period>
    <Value>
      <xsl:apply-templates/>
    </Value>
      <xsl:apply-templates mode="extract" select=
      "following-sibling::*[1]
        [starts-with(name(),'W_')
       and
        substring-after(substring-after(name(),'_'),'_')='Spots'
         ]
       "/>
  </Expenditure>
 </xsl:template>

 <xsl:template mode="extract" match=
  "*[starts-with(name(),'W_')
  and
   substring-after(substring-after(name(),'_'),'_')='Spots'
    ]
  ">
   <Spots><xsl:value-of select="."/></Spots>
  </xsl:template>


 <xsl:template match=
  "*[starts-with(name(),'W_')
  and
   substring-after(substring-after(name(),'_'),'_')='Spots'
    ]
  "/>
</xsl:stylesheet>

when applied on the provided source XML document:

<RowSet>
 <Row>
  <Location_Long_Desc>Sydney Office</Location_Long_Desc>
  <Location_Code>SYDNEY</Location_Code>
  <Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
  <Daypart_Code>PEANIG</Daypart_Code>
  <W_20050703_Dlr>30849.3</W_20050703_Dlr>
  <W_20050703_Spots>9</W_20050703_Spots>
  <W_20050710_Dlr>16.35</W_20050710_Dlr>
  <W_20050710_Spots>19</W_20050710_Spots>
 </Row>
</RowSet>

produces the wanted, correct result:

<RowSet>
   <Row>
      <Location_Long_Desc>Sydney Office</Location_Long_Desc>
      <Location_Code>SYDNEY</Location_Code>
      <Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
      <Daypart_Code>PEANIG</Daypart_Code>
      <Expenditure>
         <Period>20050703</Period>
         <Value>30849.3</Value>
         <Spots>9</Spots>
      </Expenditure>
      <Expenditure>
         <Period>20050710</Period>
         <Value>16.35</Value>
         <Spots>19</Spots>
      </Expenditure>
   </Row>
</RowSet>

when applied on the second provided XML document, requested in an Update by the OP:

<RowSet>
 <Row>
  <Location_Long_Desc>Sydney Office</Location_Long_Desc>
  <Location_Code>SYDNEY</Location_Code>
  <Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
  <Daypart_Code>PEANIG</Daypart_Code>
  <W_20050703_Dlr>30849.3</W_20050703_Dlr>
  <W_20050710_Dlr>16.35</W_20050710_Dlr>
  <W_20050710_Spots>19</W_20050710_Spots>
 </Row>
</RowSet>

again the wanted, correct result (No <Spot> element is generated if the immediate sibling isn't a W_nnnnnnnn_Spots) is produced:

<RowSet>
   <Row>
      <Location_Long_Desc>Sydney Office</Location_Long_Desc>
      <Location_Code>SYDNEY</Location_Code>
      <Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
      <Daypart_Code>PEANIG</Daypart_Code>
      <Expenditure>
         <Period>20050703</Period>
         <Value>30849.3</Value>
      </Expenditure>
      <Expenditure>
         <Period>20050710</Period>
         <Value>16.35</Value>
         <Spots>19</Spots>
      </Expenditure>
   </Row>
</RowSet>

Do note:

  1. The use of the identity rule to copy any node "as-is".

  2. The overriding of the identity template only for W_nnnnnnnn_Dlr elements.

  3. The overriding of the identity template with an empty template matching W_nnnnnnnn_Spots elements.

  4. The use of the standard XPath functions: name(), starts-with() and substring-after()

  5. The function ends-with() is only available in XPath 2.0 (XSLT 2.0) and isn't used in this XSLT 1.0 solution.

Upvotes: 5

Daniel Haley
Daniel Haley

Reputation: 52888

Here's an answer similar to Dimitre's. I'd already written it, so I thought I'd go ahead and post it...

XSLT (2.0)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:choose>
      <xsl:when test="starts-with(local-name(), 'W_') and ends-with(local-name(), '_Dlr')">
        <xsl:variable name="period" select="substring(local-name(),3,8)"/>
        <Expenditure>
          <Period><xsl:value-of select="$period"/></Period>
          <Value><xsl:apply-templates/></Value>
          <Spots><xsl:value-of select="following-sibling::*[starts-with(local-name(), 'W_') and ends-with(local-name(),concat($period,'_Spots'))]"/></Spots>
        </Expenditure>
      </xsl:when>
      <xsl:when test="ends-with(local-name(), '_Spots')"/>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Also, if you're using XSLT 2.0 (I assumed you were since you used ends-with()) you can use tokenize() to capture pieces of the name.

Example:

<xsl:variable name="period" select="tokenize(local-name(),'_')[2]"/>

Upvotes: 1

ndim
ndim

Reputation: 37905

I would start with a template like

<xsl:template match="Row/*[starts-with(name(), 'W_') and
                           ends-with(name(), '_Dlr')]">

which should more precisely match the element you want matched. As to how to select the adjacent <W_${DATE}_Spots> sibling element... why not use the following-sibling XPath axis with the proper string?

<xsl:template match="Row/*[starts-with(local-name(), 'W_') and
                           ends-with(local-name(), '_Dlr')]">
  <xsl:variable name="date" select="substring(local_name(),3,8)"/>
  ...
  <xsl:apply-templates select="following-sibling::*[local-name() ==
                               concat('W_', concat($date, '_Spots'))]"/>
  ...
</xsl:template>

<xsl:template match="Row/*[starts-with(local-name(), 'W_') and
                          ends-with(local-name(), '_Spots')]">
  <xsl:variable name="date" select="substring(local_name(),3,8)"/>
  ...
</xsl:template>

BTW, this looks like an endless loop waiting to happen:

<xsl:template match="*">
   ...
   <xsl:apply-templates select="node()"/>
   ...
</xsl:template>

I am sure there are a few mistakes in my answer, but it should be helpful in some capacity anyway.

Upvotes: 1

Related Questions