Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old April 17th, 2010, 01:46 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is some XSLT 2.0 solution that should work for the original input you posted:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  version="2.0">
  
  <xsl:param name="input-separators" as="xs:string" select="'~'"/>
  <xsl:param name="output-separator" as="xs:string" select="','"/>
  <xsl:param name="lf" as="xs:string" select="'
'"/>
  
  <xsl:output method="text"/>
  
  <xsl:function name="mf:make-lines" as="xs:string*">
    <xsl:param name="items" as="item()*"/>
    <xsl:param name="elements" as="element()*"/>
    <xsl:choose>
      <xsl:when test="$elements">
        <xsl:for-each select="tokenize($elements[1], $input-separators)">
          <xsl:sequence select="mf:make-lines(($items, .), $elements[position() gt 1])"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="concat(string-join($items, $output-separator), $lf)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>
  
  <xsl:template match="/">
    <xsl:value-of select="mf:make-lines((), RecordSet/Data/*)" separator=""/>
  </xsl:template>

</xsl:stylesheet>
When applied to the input
Code:
<RecordSet>
<Data>
<Field1>Field1</Field1>
<Field2>Field2</Field2>
<Field34>Field3~Field4</Field34>
<Field56>Field5~Field6</Field56>
<Field7>Field7</Field7>
<Field89>Field8~Field9</Field89>
<Field10>Field10</Field10>
</Data>
</RecordSet>
the output is
Code:
Field1,Field2,Field3,Field5,Field7,Field8,Field10
Field1,Field2,Field3,Field5,Field7,Field9,Field10
Field1,Field2,Field3,Field6,Field7,Field8,Field10
Field1,Field2,Field3,Field6,Field7,Field9,Field10
Field1,Field2,Field4,Field5,Field7,Field8,Field10
Field1,Field2,Field4,Field5,Field7,Field9,Field10
Field1,Field2,Field4,Field6,Field7,Field8,Field10
Field1,Field2,Field4,Field6,Field7,Field9,Field10
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 17th, 2010, 04:42 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

My original code had

Code:
<xsl:variable name="f1v" select="."/>
but you changed it to

Code:
<xsl:variable name="f1v" select="$Data/Audit_Auditees"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 17th, 2010, 06:11 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin for the code. Will try to work on that sample too. But i will have to convert into XSLT 1.0 and then test to see.
 
Old April 17th, 2010, 06:13 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
My original code had

Code:
<xsl:variable name="f1v" select="."/>
but you changed it to

Code:
<xsl:variable name="f1v" select="$Data/Audit_Auditees"/>
Yes, I changed the code back to original. It seems working. I am getting some new errors as below. I am using the source XML from the above post Not sure how to get rid of this error. Appreciate your help

java.lang.RuntimeException: Error: at xsl:for-each on line 14 column 69 of Desktop/Projects/Audit/SPLITT%7E1.XSL:
FORX0003: The regular expression in tokenize() must not be one that matches a zero-length string

at com.exln.stylus.CSaxon8Driver.doProcessing(CSaxon8 Driver.java:269)
at com.exln.stylus.CProcessorDriver.process(CProcesso rDriver.java:104)
Splitting.xsl (14, 69)
 
Old April 17th, 2010, 06:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

java.lang.RuntimeException: Error: at xsl:for-each on line 14 column 69 of Desktop/Projects/Audit/SPLITT%7E1.XSL:
FORX0003: The regular expression in tokenize() must not be one that matches a zero-length string

The error message means exactly what it says, and it points to the exact location where the error occurs. So you really ought to be able to work this one out for yourself.

The problem is tokenize(X, '|'). Vertical bar is a special character in regular expressions and needs to be escaped with a backslash.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 17th, 2010, 06:35 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am acting dumb, i know. I couldnt help myself because i have no previous experience with XSLT. I am really sorry being pain over the weekend.

I have the code running fine. Resulting nothing. No output

HTML Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="text"/>

	<xsl:template match="/">
		<xsl:variable name="Data" select="name/Recordset/Data"/>
		<xsl:for-each select="$Data">
			<xsl:variable name="this" select="."/>
			<xsl:for-each select="tokenize($this/Audit_Auditees,'~')">
				<xsl:variable name="f1v" select="."/>
				<xsl:for-each select="tokenize($this/Audit_Auditors,'~')">
					<xsl:variable name="f2v" select="."/>
					<xsl:for-each select="tokenize($this/Audit_Device,',')">
						<xsl:variable name="f3v" select="."/>
						<xsl:for-each select="tokenize($this/Audit_Lead_Auditor,'\|')">
							<xsl:variable name="f4v" select="."/>
							<xsl:for-each select="tokenize($this/Audit_Sub_System,'\|')">
								<xsl:variable name="f5v" select="."/>
								<xsl:for-each select="tokenize($this/Audit_Sub_Type,',')">
									<xsl:variable name="f6v" select="."/>
									<xsl:for-each select="tokenize($this/Closing_Meeting_Attendees,'~')">
										<xsl:variable name="f7v" select="."/>
										<xsl:for-each select="tokenize($this/MPG_Audit_Site,'\|')">
											<xsl:variable name="f8v" select="."/>
											<xsl:for-each select="tokenize($this/MPG_Auditee_Organization,',')">
												<xsl:variable name="f9v" select="."/>
												<xsl:for-each select="tokenize($this/MPG_Auditing_Body,'\|')">
													<xsl:variable name="f10v" select="."/>
													<xsl:for-each select="tokenize($this/MPG_Supplier_Capabilities,',')">
														<xsl:variable name="f11v" select="."/>
														<xsl:for-each select="tokenize($this/Opening_Meeting_Attendees,'~')">
															<xsl:variable name="f12v" select="."/>
															<xsl:value-of select="concat($f1v, '{//}', $f2v, '{//}', $f3v, '{//}', $f4v, '{//}', $f5v, '{//}', $f6v, '{//}', $f7v, '{//}', $f8v, '{//}', $f9v, '{//}', $f10v, '{//}', $f11v, '{//}', $f12v, '{//}', '&#xA;')"/>
														</xsl:for-each>
													</xsl:for-each>
												</xsl:for-each>
											</xsl:for-each>
										</xsl:for-each>
									</xsl:for-each>
								</xsl:for-each>
							</xsl:for-each>
						</xsl:for-each>
					</xsl:for-each>
				</xsl:for-each>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="output-tokens">
		<xsl:param name="list"/>
		<xsl:param name="delimiter"/>
		<xsl:variable name="newlist">
			<xsl:choose>
				<xsl:when test="contains($list, $delimiter)">
					<xsl:value-of select="normalize-space($list)"/>
				</xsl:when>

				<xsl:otherwise>
					<xsl:value-of select="concat(normalize-space($list), $delimiter)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="first" select="substring-before($newlist, $delimiter)"/>
		<xsl:variable name="remaining" select="substring-after($newlist, $delimiter)"/>
		<xsl:call-template name="replaceCharsInString">
			<xsl:with-param name="stringIn" select="string($first)"/>
			<xsl:with-param name="charsIn" select="'|'"/>
			<xsl:with-param name="charsOut" select="'{//}'"/>
		</xsl:call-template>
		<xsl:if test="$remaining">
			<xsl:call-template name="output-tokens">
				<xsl:with-param name="list" select="$remaining"/>
				<xsl:with-param name="delimiter">
					<xsl:value-of select="$delimiter"/>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template name="replaceCharsInString">
		<xsl:param name="stringIn"/>
		<xsl:param name="charsIn"/>
		<xsl:param name="charsOut"/>
		<xsl:choose>
			<xsl:when test="contains($stringIn,$charsIn)">
				<xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)"/>
				<xsl:call-template name="replaceCharsInString">
					<xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>
					<xsl:with-param name="charsIn" select="$charsIn"/>
					<xsl:with-param name="charsOut" select="$charsOut"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$stringIn"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>

Last edited by chilly; April 17th, 2010 at 06:38 PM..
 
Old April 17th, 2010, 06:47 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:for-each select="tokenize($this/MPG_Supplier_Capabilities,',')">


MPG_Supplier_Capabilities is a zero-length string, so tokenize produces an empty sequence, so the code inside this xsl:for-each is executed zero times, so there is no output.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 17th, 2010, 07:02 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If one tag misses or zero length like MPG_Supplier_Capabilities, nothing gets generated?

If yes, how do we make sure rest continues with out skipping the whole recordSet?
 
Old April 19th, 2010, 11:41 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin
Thanks for the code. Your example works with the mock up code i created. But when i execute with the real data (Which was posted here in this forum), it won't work.

Because for each field, the separator might be different in real time scenario. Also i have fields before, middle and after tokenize fields. I need more control over to customize i guess. Also using XSLT 1.0.

I am not experience, so i have no idea on how to convert to 1.0

I appreciate your help

Quote:
Originally Posted by Martin Honnen View Post
Here is some XSLT 2.0 solution that should work for the original input you posted:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  version="2.0">
  
  <xsl:param name="input-separators" as="xs:string" select="'~'"/>
  <xsl:param name="output-separator" as="xs:string" select="','"/>
  <xsl:param name="lf" as="xs:string" select="'
'"/>
  
  <xsl:output method="text"/>
  
  <xsl:function name="mf:make-lines" as="xs:string*">
    <xsl:param name="items" as="item()*"/>
    <xsl:param name="elements" as="element()*"/>
    <xsl:choose>
      <xsl:when test="$elements">
        <xsl:for-each select="tokenize($elements[1], $input-separators)">
          <xsl:sequence select="mf:make-lines(($items, .), $elements[position() gt 1])"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="concat(string-join($items, $output-separator), $lf)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>
  
  <xsl:template match="/">
    <xsl:value-of select="mf:make-lines((), RecordSet/Data/*)" separator=""/>
  </xsl:template>

</xsl:stylesheet>
When applied to the input
Code:
<RecordSet>
<Data>
<Field1>Field1</Field1>
<Field2>Field2</Field2>
<Field34>Field3~Field4</Field34>
<Field56>Field5~Field6</Field56>
<Field7>Field7</Field7>
<Field89>Field8~Field9</Field89>
<Field10>Field10</Field10>
</Data>
</RecordSet>
the output is
Code:
Field1,Field2,Field3,Field5,Field7,Field8,Field10
Field1,Field2,Field3,Field5,Field7,Field9,Field10
Field1,Field2,Field3,Field6,Field7,Field8,Field10
Field1,Field2,Field3,Field6,Field7,Field9,Field10
Field1,Field2,Field4,Field5,Field7,Field8,Field10
Field1,Field2,Field4,Field5,Field7,Field9,Field10
Field1,Field2,Field4,Field6,Field7,Field8,Field10
Field1,Field2,Field4,Field6,Field7,Field9,Field10
 
Old April 19th, 2010, 11:43 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
<xsl:for-each select="tokenize($this/MPG_Supplier_Capabilities,',')">


MPG_Supplier_Capabilities is a zero-length string, so tokenize produces an empty sequence, so the code inside this xsl:for-each is executed zero times, so there is no output.
mhkay
Does the code works in XSLT 1.0 compiler straight? Also how to skip the fields those were empty with out resulting into no output.

Also while doing tokenize, i like to replace special char from some of the fields. Like below example, tokenize splits Autitee 1|Title1, i would like to replace PIPE to {//}, please advice

<Audit_Auditees>Auditee 1|Title 1~Auditee 2|Title 2~Auditee 3|Title 3</Audit_Auditees>


Appreciate your great help here.

Last edited by chilly; April 19th, 2010 at 11:45 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
errors with nested Do While Loops hddavie BOOK: Beginning ASP 3.0 0 August 5th, 2009 04:20 PM
Flat XML to nested IgorK XSLT 10 November 24th, 2008 09:56 AM
Convert nested XML to flat one dani1 XSLT 15 October 21st, 2008 02:20 AM
weird program flow with nested loops zayasv Intro Programming 2 November 17th, 2005 06:19 AM
problem with nested loops ptaylor2005 Beginning PHP 4 April 27th, 2005 07:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.