Solved

How to concatenate 2 fields into 1 field in xml with xsl or other

Posted on 2014-04-09
9
597 Views
Last Modified: 2014-04-10
Trying to merge the fields OrderField1 and JobType into the OrderField1 field.  
So if
OrderField1="PW" and jobType="Generic"

The end result would be OrderField1="PW-Generic"
OrderExport.xml
0
Comment
Question by:Knight905416
  • 5
  • 4
9 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39989604
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="/order">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:attribute name="OrderField1">
                <xsl:value-of select="@OrderField1"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="job/@jobType"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@*">
        <xsl:copy-of select="."/>
    </xsl:template>
    
    
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39989608
The only tricky thing in the template is the

           <xsl:apply-templates select="@*"/>
            <xsl:attribute name="OrderField1">

where you would have two attribute definitions of OrderField1 in a row,
but the second overwrites the first, so order is important
(assuming XSLT1)
0
 

Author Comment

by:Knight905416
ID: 39989669
Ok, thank you, looks like its moving in the right direction.  The only gap in my knowledge is how do I link the code with the xml.  Should there be a reference in the xsl?  Should the code be in the xml document?  Should the xsl file be in the same directory?  Is there a reference in the xml document pointing to the xsl document, etc?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39989721
XSLT will transform XML into something else, could be XML as is the case here.

Making a reference to the XSL from within the XML only works if you need rendering in a browser, so you can't do that here

You need an XSLT processor, something that takes the XML, and the XSLT and returns the result XML

You need to tell me where you want to build that in, in a programming language? As a command line process? Basically one way or another you need to execute the XSLT against an XSLT processor
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Knight905416
ID: 39989765
I guess we could use VB.Net or something.  I was hoping that XSLT could do this transformation.  I will attach my 2 files.  They produce nothing yet.
OrderExportTest1.xml
OrderExportTest1.xsl
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39989838
well, your XSLT works perfectly on your source document

BUT as I said before, the xml stylesheet processing instruction is only usefull when you want to render in a browser

If you can use VB.net
http://www.codeproject.com/Questions/597678/ApplyplusXSLplustransformationplusonplusXMLplus-pl
google for vb.net + xslt, you will find a zillion of examples on how to do that
(I myself am not a VB developer)
0
 

Author Comment

by:Knight905416
ID: 39989898
I'm getting a completely blank browser with those files - tried in IE, Firefox and Chrome.
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 39989955
Do you read what I write?

I told you twice allready that that won't work

I have tested the XSLT with a number of XSLT processors... the XSLT simply works

But if you use a xml-stylesheet instruction (which as I said only works for rendering the transformation result in a browser) you will see nothing. The reason is simple. Your target XML has a number of tags, but none are known html tags. Browsers ignore the elements they don't know, means that each browser will ignore the transformations result and show you a blank page. No surprise there

If you need the XML result as you put in your question, this implies an XML end result... well, then a browser can't help you
0
 

Author Closing Comment

by:Knight905416
ID: 39991890
Do you read what I write?  I love it.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now