 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 8th, 2010, 06:03 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to get preceding node child element value
[FONT='Arial','sans-serif']Folks, I need your help:[/FONT]
[FONT='Arial','sans-serif']I am trying to get the preceding node child element value (sourceList/source/sourceID/text(). I am in a for loop but need two values based upon some specific condition but I am not getting the expected result. The variable is coming back with blank. [/FONT]
[FONT='Arial','sans-serif']Also, my xml has a namespace so the root element is not firing when I do template match. I temporarily removed the namespace so it is working in order to make some progress.[/FONT]
[FONT='Arial','sans-serif']Please help with the preceding node issue and namespace.[/FONT]
[FONT='Arial','sans-serif']XML with out the name space (this is the one that I am using to get the preceding node value[/FONT]
<?xml version="1.0"?>
<MultiSpeakMessageHeader>
<TimeStamp>2010-05-25T09:25:08.440-04:00</TimeStamp>
<DocumentID>Show20100525</DocumentID>
<VendorApp>
<AppName>SerViewCom</AppName>
<AppVersion>1.60.7-12613</AppVersion>
<Company>Trilliant Networks</Company>
<Function>LP-CB</Function>
</VendorApp>
<Batch>
<MultiSpeak documentType="dump">
<profileObject objectID="891208672">
<extensions>
<to>2010-04-06T09:24:10.00-04:00</to>
<meterSerialNumber>44725237</meterSerialNumber>
<from>2010-04-05T09:24:10.00-04:00</from>
</extensions>
<sourceList>
<source>
<sourceID>1</sourceID>
<quantityType>summation</quantityType>
<uom>Wh</uom>
<accountability>Q1-4</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
<source>
<sourceID>2</sourceID>
<quantityType>summation</quantityType>
<uom>VARh</uom>
<accountability>Q1-2</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
<source>
<sourceID>3</sourceID>
<quantityType>summation</quantityType>
<uom>Wh</uom>
<accountability>Q2-3</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
<source>
<sourceID>4</sourceID>
<quantityType>summation</quantityType>
<uom>VARh</uom>
<accountability>Q3-4</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
</sourceList>
<loadProfileList>
<loadProfile>
<meterID>891208672</meterID>
<dataSetNumber>1</dataSetNumber>
<channelList>
<channel channelNumber="1" endReadingSourceID="1" intervalSourceID="1">
<blockList>
<block endReading="1.820962347134453E7" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">249.3</interval>
<interval DSTinEffect="true">246.0</interval>
</block>
</blockList>
</channel>
<channel channelNumber="2" endReadingSourceID="2" intervalSourceID="2">
<blockList>
<block endReading="5632062.233924266" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">72.0</interval>
<interval DSTinEffect="true">68.4</interval>
</block>
</blockList>
</channel>
<channel channelNumber="3" endReadingSourceID="3" intervalSourceID="3">
<blockList>
<block endReading="0.0" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">0.0</interval>
<interval DSTinEffect="true">0.0</interval>
</block>
</blockList>
</channel>
<channel channelNumber="4" endReadingSourceID="4" intervalSourceID="4">
<blockList>
<block endReading="0.0" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">0.0</interval>
<interval DSTinEffect="true">0.0</interval>
<interval DSTinEffect="true">0.0</interval>
</block>
</blockList>
</channel>
</channelList>
</loadProfile>
</loadProfileList>
</profileObject>
</MultiSpeak>
</Batch>
</MultiSpeakMessageHeader>
[FONT='Arial','sans-serif']XSLT code
[FONT='Arial','sans-serif']<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xalan/java" xmlns:lsxd="xalan://com.lodestarcorp.core.xml.xslt" exclude-result-prefixes="java lsxd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="MultiSpeakMessageHeader">
<ROOT>
<xsl:apply-templates select="Batch/MultiSpeak/profileObject"/>
</ROOT>
</xsl:template>
<!-- Meter Readings Template -->
<xsl:template match="Batch/MultiSpeak/profileObject">
<!-- Parameter Names -->
<xsl:param name="effectiveDate" select="extensions/to"/>
<xsl:param name="startReadTime" select="extensions/from"/>
<xsl:for-each select="loadProfileList/loadProfile/channelList/channel/blockList">
<xsl:variable name="stopval" select="block/@endReading"/>
<xsl:variable name="spi" select="block/@intervalPeriod * 60"/>
<!--<xsl:variable name="rawDcflow" select="block/@intervalPeriod * 60"/>-->
<xsl:variable name="meterID" select="../../../meterID"/>
<xsl:variable name="testID" select="preceding::sourceList/source/sourceID/text()"/>
<xsl:variable name="sourceID" select="../@intervalSourceID"/>
<xsl:variable name="rawuomName">
<xsl:for-each select="preceding::sourceList/source">
<xsl:text>scott</xsl:text>
<!-- sourceList/source sourceID/text() -->
<xsl:if test="sourceID/text() = $sourceID">
<xsl:value-of select="uom"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="rawDCflow">
<xsl:for-each select="preceding::sourceList/source">
<xsl:if test="sourceID/text() = $sourceID">
<xsl:value-of select="accountability"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="dcflowDecode">
<xsl:call-template name="decodeDCFLOW">
<xsl:with-param name="rawDCFlowName" select="$rawDCflow"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="uomCode">
<xsl:call-template name="decodeUom">
<xsl:with-param name="uomName" select="$rawuomName"/>
</xsl:call-template>
</xsl:variable>
<BIRD>
<xsl:attribute name="HEADERSERIALNUMBER"><xsl:value-of select="$meterID"/></xsl:attribute>
<xsl:attribute name="CHANNELID"><xsl:value-of select="$sourceID"/></xsl:attribute>
<xsl:attribute name="UOMCODE"><xsl:value-of select="$uomCode"/></xsl:attribute>
<xsl:attribute name="USAGETYPECODE"><xsl:text>INTERVAL</xsl:text></xsl:attribute>
<xsl:attribute name="EFFECTIVEDATE"><xsl:value-of select="$effectiveDate"/></xsl:attribute>
<METERREADS>
<METERREAD>
<xsl:attribute name="SPI"><xsl:value-of select="$spi"/></xsl:attribute>
<xsl:attribute name="STOPVAL"><xsl:value-of select="$stopval"/></xsl:attribute>
<xsl:attribute name="STARTREADTIME"><xsl:value-of select="$startReadTime"/></xsl:attribute>
<xsl:attribute name="STOPREADTIME"><xsl:value-of select="$effectiveDate"/></xsl:attribute>
<xsl:attribute name="DCFLOW"><xsl:value-of select="$dcflowDecode"/></xsl:attribute>
<!-- Hardcoded DSTPARTICIPANT and TZSTDNAME -->
<xsl:attribute name="DSTPARTICIPANT"><xsl:text>Y</xsl:text></xsl:attribute>
<xsl:attribute name="ORIGIN"><xsl:text>M</xsl:text></xsl:attribute>
<xsl:attribute name="SERIALNUMBER"><xsl:value-of select="$meterID"/></xsl:attribute>
<xsl:attribute name="READDATE"><xsl:value-of select="$effectiveDate"/></xsl:attribute>
<INTS>
<xsl:for-each select="block/interval">
<xsl:variable name="rawReading">
<xsl:value-of select="text()"/>
</xsl:variable>
<I>
<xsl:attribute name="V"><xsl:value-of select="$rawReading"/></xsl:attribute>
</I>
</xsl:for-each>
</INTS>
</METERREAD>
</METERREADS>
</BIRD>
</xsl:for-each>
<!--<xsl:apply-templates/>-->
</xsl:template>
[FONT='Arial','sans-serif']XML with name space (this is the one where the root element will not fire
<?xml version="1.0"?>
<MultiSpeakMessageHeader xmlns="http://www.multispeak.org/Schema/Version2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.2">
<TimeStamp>2010-05-25T09:25:08.440-04:00</TimeStamp>
<DocumentID>Show20100525</DocumentID>
<VendorApp>
<AppName>SerViewCom</AppName>
<AppVersion>1.60.7-12613</AppVersion>
<Company>Trilliant Networks</Company>
<Function>LP-CB</Function>
</VendorApp>
<Batch>
<MultiSpeak documentType="dump">
<profileObject objectID="891208672">
<extensions>
<to>2010-04-06T09:24:10.00-04:00</to>
<meterSerialNumber>44725237</meterSerialNumber>
<from>2010-04-05T09:24:10.00-04:00</from>
</extensions>
<sourceList>
<source>
<sourceID>1</sourceID>
<quantityType>summation</quantityType>
<uom>Wh</uom>
<accountability>Q1-4</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
<source>
<sourceID>2</sourceID>
<quantityType>summation</quantityType>
<uom>VARh</uom>
<accountability>Q1-2</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
<source>
<sourceID>3</sourceID>
<quantityType>summation</quantityType>
<uom>Wh</uom>
<accountability>Q2-3</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
<source>
<sourceID>4</sourceID>
<quantityType>summation</quantityType>
<uom>VARh</uom>
<accountability>Q3-4</accountability>
<transformerRatio>
<applied>false</applied>
<ct>1.0</ct>
<pt>1.0</pt>
</transformerRatio>
<displayFormat>
<supressLeadingZero>true</supressLeadingZero>
<numberOfDigits>5</numberOfDigits>
<precision>0</precision>
<displayMultiplier>1000.0</displayMultiplier>
</displayFormat>
<multiplierApplied>false</multiplierApplied>
</source>
</sourceList>
<loadProfileList>
<loadProfile>
<meterID>891208672</meterID>
<dataSetNumber>1</dataSetNumber>
<channelList>
<channel channelNumber="1" endReadingSourceID="1" intervalSourceID="1">
<blockList>
<block endReading="1.820962347134453E7" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">249.3</interval>
<interval DSTinEffect="true">246.0</interval>
</block>
</blockList>
</channel>
<channel channelNumber="2" endReadingSourceID="2" intervalSourceID="2">
<blockList>
<block endReading="5632062.233924266" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">72.0</interval>
<interval DSTinEffect="true">68.4</interval>
</block>
</blockList>
</channel>
<channel channelNumber="3" endReadingSourceID="3" intervalSourceID="3">
<blockList>
<block endReading="0.0" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">0.0</interval>
<interval DSTinEffect="true">0.0</interval>
</block>
</blockList>
</channel>
<channel channelNumber="4" endReadingSourceID="4" intervalSourceID="4">
<blockList>
<block endReading="0.0" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">0.0</interval>
<interval DSTinEffect="true">0.0</interval>
<interval DSTinEffect="true">0.0</interval>
</block>
</blockList>
</channel>
</channelList>
</loadProfile>
</loadProfileList>
</profileObject>
</MultiSpeak>
</Batch>
</MultiSpeakMessageHeader>
[/FONT][/FONT][/FONT]
Last edited by smandeh; June 8th, 2010 at 06:09 PM..
|
|

June 8th, 2010, 06:40 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
For the namespace problem, please google "FAQ XSLT default namespace".
For the other problem, please supply a simplified example. There's much to much code here for anyone to wade through. You'll find it easier to understand the problem yourself if you reduce it to essentials.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

June 8th, 2010, 10:00 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
For some reason I can not edit my previous post so I had posted a shorter version of my xml and xslt.
xml
<?xml version="1.0" encoding="UTF-8"?>
<MultiSpeakMessageHeader>
<Batch>
<MultiSpeak documentType="dump">
<profileObject objectID="891208672">
<sourceList>
<source>
<sourceID>1</sourceID>
<quantityType>summation</quantityType>
<uom>Wh</uom>
<accountability>Q1-4</accountability>
</source>
<source>
<sourceID>2</sourceID>
<quantityType>summation</quantityType>
<uom>VARh</uom>
<accountability>Q1-2</accountability>
</source>
</sourceList>
<loadProfileList>
<loadProfile>
<meterID>891208672</meterID>
<dataSetNumber>1</dataSetNumber>
<channelList>
<channel channelNumber="1" endReadingSourceID="1" intervalSourceID="1">
<blockList>
<block endReading="1.820962347134453E7" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">249.3</interval>
<interval DSTinEffect="true">246.0</interval>
</block>
</blockList>
</channel>
<channel channelNumber="2" endReadingSourceID="2" intervalSourceID="2">
<blockList>
<block endReading="5632062.233924266" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">72.0</interval>
<interval DSTinEffect="true">68.4</interval>
</block>
</blockList>
</channel>
</channelList>
</loadProfile>
</loadProfileList>
</profileObject>
</MultiSpeak>
</Batch>
</MultiSpeakMessageHeader>
xslt code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xalan/java" xmlns:lsxd="xalan://com.lodestarcorp.core.xml.xslt" exclude-result-prefixes="java lsxd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="MultiSpeakMessageHeader">
<ROOT>
<xsl:apply-templates select="Batch/MultiSpeak/profileObject"/>
</ROOT>
</xsl:template>
<!-- Meter Readings Template -->
<xsl:template match="Batch/MultiSpeak/profileObject">
<xsl:for-each select="loadProfileList/loadProfile/channelList/channel/blockList">
<xsl:variable name="stopval" select="block/@endReading"/>
<xsl:variable name="spi" select="block/@intervalPeriod * 60"/>
<xsl:variable name="sourceID" select="../@intervalSourceID"/>
<xsl:variable name="rawuomName">
<xsl:for-each select="preceding::sourceList/source">
<xsl:if test="sourceID/text() = $sourceID">
<xsl:value-of select="uom"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
</xsl:template>
|
|

June 9th, 2010, 06:12 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
There are no relevant templates to the below template calls :
Code:
<xsl:call-template name="decodeDCFLOW">
<xsl:call-template name="decodeUom">
Your need is bit unclear. Post the required output you need after transformation.
__________________
Rummy
|
|

June 9th, 2010, 08:38 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Rummy,
As per Kay suggestion I posted a shorter verion of the code. Below is a clearer version of my input file: All I am trying to do is how to get the SourceList/Source/SourceID/text() from loadProfileList/loadProfile/channelList/channel/blockList in a for loop based upon a test condition. Because the SourceList has multiple source nodes, I have to loop through it to get the value that I want.
<?xml version="1.0" encoding="UTF-8" ?>
- <MultiSpeakMessageHeader>
- <Batch>
- <MultiSpeak documentType="dump">
- <profileObject objectID="891208672">
- <sourceList>
- <source>
<sourceID>1</sourceID>
<quantityType>summation</quantityType>
<uom>Wh</uom>
<accountability>Q1-4</accountability>
</source>
- <source>
<sourceID>2</sourceID>
<quantityType>summation</quantityType>
<uom>VARh</uom>
<accountability>Q1-2</accountability>
</source>
</sourceList>
- <loadProfileList>
- <loadProfile>
<meterID>891208672</meterID>
<dataSetNumber>1</dataSetNumber>
- <channelList>
- <channel channelNumber="1" endReadingSourceID="1" intervalSourceID="1">
- <blockList>
- <block endReading="1.820962347134453E7" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">249.3</interval>
<interval DSTinEffect="true">246.0</interval>
</block>
</blockList>
</channel>
- <channel channelNumber="2" endReadingSourceID="2" intervalSourceID="2">
- <blockList>
- <block endReading="5632062.233924266" endTime="2010-04-05T14:45:00.00-04:00" intervalPeriod="15" sequenceNumber="670">
<interval DSTinEffect="true">72.0</interval>
<interval DSTinEffect="true">68.4</interval>
</block>
</blockList>
</channel>
</channelList>
</loadProfile>
</loadProfileList>
</profileObject>
</MultiSpeak>
</Batch>
</MultiSpeakMessageHeader>
|
|

June 9th, 2010, 08:50 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
I suggest you define and use a key so put
Code:
<xsl:key name="k1" match="source" use="sourceID"/>
as a child of your xsl:stylesheet or xsl:transform element, then use the "key" function to find the "source" element you are looking for e.g.
Code:
<xsl:for-each select="loadProfileList/loadProfile/channelList/channel/blockList">
<xsl:variable name="rawuomName" select="key('k1', ../@intervalSourceID)/uom"/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

June 9th, 2010, 09:08 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
I meant the xslt code you posted first did not have relevant templates to "decodeDCFLOW" and "decodeUom".
Next, I could not find anything wrong in the code. With the below code
Code:
<xsl:template match="MultiSpeakMessageHeader">
<ROOT>
<xsl:apply-templates select="Batch/MultiSpeak/profileObject"/>
</ROOT>
</xsl:template>
<!-- Meter Readings Template -->
<xsl:template match="Batch/MultiSpeak/profileObject">
<xsl:for-each select="loadProfileList/loadProfile/channelList/channel/blockList">
<xsl:variable name="stopval" select="../@intervalSourceID"/>
<xsl:variable name="rawuomName">
<xsl:for-each select="preceding::sourceList/source[sourceID = $sourceID]">
<xsl:value-of select="uom"/>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$rawuomName"/>
</xsl:for-each>
</xsl:template>
I got the below output
Code:
<ROOT>WhVARh</ROOT>
I used the simplified input you have posted.
__________________
Rummy
|
|

June 9th, 2010, 09:50 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Martin,
Thank you so much. I used the key as you suggested and it is working as expected.
Also, thanks Kay and Rummy for your inputs.
Regards,
smandeh
|
|
 |