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 12th, 2004, 08:37 AM
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to albruan Send a message via MSN to albruan
Default XML won't transform as desired

I have an XML file I'm wanting to tranform from one format into another; however, the XSL file isn't exactly set up properly and I don't know how to troubleshoot it.

Original XML file:
<?xml version="1.0" encoding="utf-8"?>
<Transaction type="FixedAssetCount">
    <DeviceID>123444555</DeviceID>
    <User>ICSNT/JWRIGHT</User>
    <Pwd>jwright</Pwd>
    <Collected>2004-02-12-12:33:33</Collected>
    <Received>2004-02-13-12:33:33</Received>
    <DataRecord TransactionBoundary="1">
        <Params>
            <param name="AssetItemNumber">1234</param>
            <param name="AssetCostCenter">15</param>
            <param name="AssetAisleLocation">3</param>
        </Params>
    </DataRecord>
    <DataRecord TransactionBoundary="2">
        <Params>
            <param name="AssetItemNumber">1235</param>
            <param name="AssetCostCenter>23</param>
            <param name="AssetAisleLocation">10</param>
        </Params>
    </DataRecord>
</Transaction>

XSL file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:template match="/">
        <xsl:for-each select="/Transaction/DataRecord">
            <xsl:text disable-output-escaping="yes">&lt;Request pwd='</xsl:text>
            <xsl:value-of select="/Transaction/Pwd"></xsl:value-of>
            <xsl:text>' type='callmethod' user='</xsl:text>
            <xsl:value-of select="/Transaction/User"></xsl:value-of>
            <xsl:text>' session='' environment='' sessionidle=''</xsl:text>
            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>

            <xsl:text disable-output-escaping="yes">&lt;callMethod app='Transaction' name='</xsl:text>
            <xsl:for-each select="//Transaction">
                <xsl:value-of select="@type"></xsl:value-of>
                <xsl:text disable-output-escaping="yes">'&gt;</xsl:text>
            </xsl:for-each>

            <xsl:text disable-output-escaping="yes">&lt;returnCode code='</xsl:text>
            <xsl:text>0</xsl:text>
            <xsl:text disable-output-escaping="yes">'/&gt;</xsl:text>


            <xsl:text disable-output-escaping="yes">&lt;params&gt;</xsl:text>


            <xsl:text disable-output-escaping="yes">&lt;param name='AssetItemNumber'></xsl:text>
            <xsl:value-of select="//param[@name='AssetItemNumber']"></xsl:value-of>
            <xsl:text disable-output-escaping="yes">&lt;/param&gt;</xsl:text>


            <xsl:text disable-output-escaping="yes">&lt;param name='CountLocation'></xsl:text>
            <xsl:value-of select="//param[@name='AssetCostCenter']"></xsl:value-of>
            <xsl:text disable-output-escaping="yes">&lt;/param&gt;</xsl:text>


            <xsl:text disable-output-escaping="yes">&lt;param name='AisleLocation'></xsl:text>
            <xsl:value-of select="//param[@name='AssetAisleLocation']"></xsl:value-of>
            <xsl:text disable-output-escaping="yes">&lt;/param&gt;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Desired XML output after transformation:
<Request pwd='jwright' type='callmethod' user='ICSNT/JWRIGHT' session='' environment='' sessionidle=''>
    <callMethod app='Transaction' name='FixedAssetCount'>
        <returnCode code='0'/>
        <params>
            <param name='AssetItemNumber'>1234</param>
            <param name='CountLocation'>15</param>
            <param name='AisleLocation'>3</param>
        </params>
    </callMethod>
</Request>
<Request pwd='jwright' type='callmethod' user='ICSNT/JWRIGHT' session='' environment='' sessionidle=''>
    <callMethod app='Transaction' name='FixedAssetCount'>
        <returnCode code='0'/>
        <params>
            <param name='AssetItemNumber'>1235</param>
            <param name='CountLocation'>23</param>
            <param name='AisleLocation'>10</param>
        </params>
    </callMethod>
</Request>

Actual resulting XML after transformation:
<Request pwd='jwright' type='callmethod' user='ICSNT/JWRIGHT' session='' environment='' sessionidle=''>
    <callMethod app='Transaction' name='FixedAssetCount'>
        <returnCode code='0'/>
        <params>
            <param name='AssetItemNumber'>1234</param>
            <param name='CountLocation'>15</param>
            <param name='AisleLocation'>3</param>
        </params>
    </callMethod>
</Request>
<Request pwd='jwright' type='callmethod' user='ICSNT/JWRIGHT' session='' environment='' sessionidle=''>
    <callMethod app='Transaction' name='FixedAssetCount'>
        <returnCode code='0'/>
        <params>
            <param name='AssetItemNumber'>1234</param>
            <param name='CountLocation'>15</param>
            <param name='AisleLocation'>3</param>
        </params>
    </callMethod>
</Request>

Please notice that the values for AssetItemNumber, CountLocation and AisleLocation are the same in the actual XML output from the transformation process whereas they should differ as shown in the desired XML output. Any help in this would be greatly appreciated.

 
Old April 13th, 2004, 04:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

You are having this problem because you are using XPath such as this:
//param[@name='AssetCostCenter']
which just picks up the first matching node in the document, so you are always getting the same value.

I have to say this is a very strange way of creating xml, you really should be creating a result tree, not using xsl:text, to create an xml document. If I get the time later I'll post an example...
 
Old April 13th, 2004, 04:21 PM
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to albruan Send a message via MSN to albruan
Default

Thanks for looking at the code, pgtips...after reading your response, I noticed I'd omitted the . prior to //param. That's taken care of that problem.

I used xsl:text to create the transformed XML document as it's the only method of which I was aware. I have to admit it's a bit awkward, but it does the trick; however, I'd be especially appreciative if you are able to post an example of creating a result tree. Cheers!

 
Old April 14th, 2004, 04:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>


    <xsl:variable name="user" select="/Transaction/User"/>
    <xsl:variable name="pwd" select="/Transaction/Pwd"/>
    <xsl:variable name="txnType" select="/Transaction/@type"/>

    <xsl:template match="/">

        <Requests>
            <xsl:apply-templates select="Transaction/DataRecord"/>
        </Requests>
    </xsl:template>

    <xsl:template match="DataRecord">

        <Request pwd="{$pwd}" type="callmethod" user="{$user}" session="" environment="" sessionidle="">


            <callMethod app="Transaction" name="{$txnType}">

                <returnCode code="0"/>

                <xsl:apply-templates select="Params"/>

            </callMethod>
        </Request>
    </xsl:template>

    <xsl:template match="Params">
        <!-- if you wanted the same param names in the result tree you could just do <xsl:copy-of select="."/>,
        but you don't so you can't :-) -->
        <params>

            <xsl:copy-of select="param[@name='AssetItemNumber']"/>

            <param name="CountLocation"><xsl:value-of select="param[@name='AssetCostCenter']"/></param>
            <param name="AisleLocation"><xsl:value-of select="param[@name='AssetAisleLocation']"/></param>
        </params>
    </xsl:template>
</xsl:stylesheet>
If any of the above is not clear, just ask.

rgds
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform xml to xml changing one tag. surfer97301 XSLT 2 April 21st, 2010 05:14 PM
transform XML from one form to another aayamsingh XSLT 4 October 23rd, 2008 03:04 AM
XML to XML transform (Simplified) nmahesh567 XSLT 2 March 24th, 2007 07:57 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM
Transform XML to XML using XSLT Mr.D XSLT 2 September 7th, 2004 02:13 PM





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