Solved

How to pass an input to xsltproc

Posted on 2015-01-27
7
117 Views
Last Modified: 2015-01-28
I want to pass an input to xsltproc for the number function..

I am able to do it via strings to get a specific output, however i want a numbering sequence to start with what i have provided as input..

below is the sample xml and xsl . please help

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="site_name">Site1</xsl:param>
  <xsl:param name="site_number">100</xsl:param>
  <xsl:output method="text"/>
  <xsl:template match="Servers">
    <xsl:for-each select="//Server">
      <xsl:if test="Name=$site_name">
   <xsl:number format=$site_number level="any"/>
        <xsl:value-of select="User"/>
        <xsl:value-of select="Pass"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Open in new window


XML is

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Container>
    <Servers>
        <Folder expanded="1">
            <Server>
                <User>username1</User>
                <Pass>password1</Pass>
                <Name>Site1</Name>
            </Server>
            <Server>
                <User>username2</User>
                <Pass>password2</Pass>
                <Name>Site2</Name>
            </Server>
        </Folder>
        <Server>
            <User>username3</User>
            <Pass>password3</Pass>
            <Name>Site3</Name>
        </Server>
        <Server>
            <User>username4</User>
            <Pass>password4</Pass>
            <Name>Site4</Name>
        </Server>
    </Servers>
</Container>

Open in new window


expected output with the command

xsltproc --stringparam site_name "Site2" site_number "501" param.xsl param.xml

is
501username2password2
0
Comment
Question by:Wasim Akram Shaik
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40573251
<xsl:number format="{$site_number}" level="any"/>
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40573283
I misread the question

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="site_name">Site1</xsl:param>
    <xsl:param name="site_number">500</xsl:param>
    <xsl:output method="text"/>
    <xsl:template match="Servers">
        <xsl:apply-templates select="//Server[Name=$site_name]"/>
     </xsl:template>
    
    <xsl:template match="Server">
        <xsl:variable name="number">
            <xsl:number level="any"/>
        </xsl:variable>
        <xsl:value-of select="$site_number + $number"/>
        <xsl:value-of select="User"/>
        <xsl:value-of select="Pass"/>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 16

Author Comment

by:Wasim Akram Shaik
ID: 40573324
Thanks geert, i had got passed the input variable, but i have a situation here.. what am trying to achieve is you had already answered it earlier..

<xsl:number format="1" level="any" count="PersonNameDetails/PersonNames | EntityNameDetails/EntityNames"/>

what am actually trying to achieve is to go for a counter without this.. because i think the above counter is slowing down the processing speed.. (correct me if am wrong).. am trying to achieve a counter without this. where in i can increment the number sequentially..

the suggestion is again resetting the counter value.. pls suggest if you see any improvement in this..
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Author Comment

by:Wasim Akram Shaik
ID: 40574435
Geert.. Even the latest comment is giving out an error..
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 40574615
First on performance.
I am not sure xsl:number should be slow.
Most xslt processors calculate them on parsing the document (it is a straightforward optimisation)
I think it beats using position()

You could use keys to get a performance optimisation, but I am not sure it would help

I am not sure what the error is, can you give more details?
0
 
LVL 16

Author Closing Comment

by:Wasim Akram Shaik
ID: 40575387
Sorry for the delay.. Thanks for the answer..

Can you please shed some light on xsl:key, is it possible to generate sequence numbers

basically what am doing is am transforming a xml document to txt file

i just need a counter for every new line.. can key do this?, what i thought was counter using the <xsl:number> is impacting performance..

actually the file size is 38 mb.. its generating approximately 20k records with 35 columns overall(12 has data in them, rest are null and tab delimited)

its taking almost 22min 17 secs to complete.. thats where am looking out the bottlenecks so as to reduce this burden.. first i could see is the counter.. which when removed had given a improvement of around 5 mins.. ie., if there is no counter then xsltproc is completing in 17mins..
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40575973
I can't imagin why xslt-proc is working 22 minutes on a 35MB file
I am curious what your environment is, moving to saxon and XSLT 2 could help a fair amount, XSLT-Proc is really old
If you are running this from one of the C-languages (ruby, php, perl) however, there is little alternative

An approach that most likely will boost performance is seperation of concerns
Make your XSLT a multiple step process
- a first XSLT could be an identity transform that removes all the null values, the result wil be a substantially smaller XML
- use that XML to do all the required processing in a second step, including the numbers
Make sure that you don't serialise between steps
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question