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 July 2nd, 2017, 05:32 PM
Registered User
 
Join Date: Jul 2017
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Question Problems dealing with xml sporadic and repetitive tags using xsl

I have the following XML file I am converting to csv, the problem is, the first data section is all string tags which I can skip if I need and the second section is a mix of string and int tags. i am able to get the data out, but there is a weird block of data on top i am unable to remove. The XML file is automatically generated so I cant change it.

<?xml version="1.0" encoding="utf-8"?>
<methodResponse><params><param><value>
<struct>
<member><name>columns</name><value>
<array><data>
<value><string>id</string></value>
<value><string>scanTime</string></value>
<value><string>host</string></value>
<value><string>vuln</string></value>
<value><string>port</string></value>
<value><string>protocol</string></value>
</data></array>
</value></member>
<member><name>table</name><value>
<array><data>
<value>
<array><data>
<value><int>1</int></value>
<value><int>1414010812</int></value>
<value><string>Host.5</string></value>
<value><string>Vuln.6230</string></value>
<value><int>500</int></value>
<value><string>udp</string></value>
</data></array>
</value>
<value>
</data></array>
</value></member>
</struct></value>
</param>
</params>
</methodResponse>

my XSL 1.0 code below

<xsl:variable name="new-line" select="'
'"/>
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="value/array/data">
<xsl:for-each select="//array/data">
<xsl:for-each select="value">
<xsl:value-of select="translate(*, $new-line,' ')"/>,
</xsl:for-each><br/>;
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

my desired output

id,scanTime,host,vuln,port,protocol
1,1414010812,Host.5,Vuln.6230,500,udp;
2,1414010978,Host.6,Vuln.1191,22,tcp;
3,1414010978,Host.6,Vuln.30535,22,tcp;
4,1414010978,Host.6,Vuln.78682,22,tcp;
5,1414010978,Host.6,Vuln.78683,22,tcp;

The problem is that it runs through the process and prints everything on one line with huge spaces before it prints my desired output.

I have read the best practices for posting, however this is my first post so feel free leave any solid feedback for posting etiquette. Thank you
 
Old July 2nd, 2017, 05:52 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, I'd suggest that unless your question is specifically about the content of a Wrox/Wiley book, you're probably better off posting the question on StackOverflow these days. That's where you are most likely to get an answer.

Your code does

Code:
<xsl:template match="value/array/data">
  <xsl:for-each select="//array/data">
which means that whenever a data element is encountered, you are processing every data element in the whole document.

In addition, you're applying the default template rules to whitespace text nodes in the source, which causes the whitespace to be copied to the output.

You're also attempting to generate newlines by outputting a br element, but that only makes sense when generating HTML: the text output method will ignore empty elements, it won't translate them into newlines.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Leothedog (July 2nd, 2017)
 
Old July 2nd, 2017, 06:24 PM
Registered User
 
Join Date: Jul 2017
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Thank you

Thank you for the reply and good tips





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dealing with the presence of an XML tag rather tha Mike250 SQL Server 2005 1 June 6th, 2008 03:50 PM
repetitive xml - sorry, I'm new to this... JamesHurrell XSLT 4 March 12th, 2008 04:24 PM
XSL:FO Querying block id tags goldendase XSLT 4 September 20th, 2007 02:54 PM
XSL containing JSTL tags! techfreak123 XSLT 5 August 1st, 2006 09:50 PM
namespaces appearing in tags after XSL translation mphare XSLT 4 February 20th, 2006 03:49 PM





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