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 February 22nd, 2007, 04:45 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help with xsl file creation

Hi,

I need to convert the following
<Item>
<Attribute name="PRODUCTID">
  <Value>171</Value>
</Attribute>
<Attribute name="ITEM_CREATE">
  <Value/>
</Attribute>
<Attribute name="ITEM_PAY2">
  <Value>[{x=, y=, z=-1}]</Value>
</Attribute>
</Item>

into
<Item>
<PRODUCTID>171</PRODUCTID>
<ITEM_CREATE></ITEM_CREATE>
<ITEM_PAY2>
 <x></x>
 <y></y>
 <z>-1</z>
</ITEM_PAY2>
</Item>

am using the following working syntax
<xsl:template match="/">
<xsl:for-each select="Item/Attribute">
            <xsl:variable name="Val" select="string( . )" />
              <xsl:variable name="Attr" select="string(@name )" />
             <xsl:element name="{$Attr}">
                  <xsl:value-of select="$Val" />
              </xsl:element>
          </xsl:for-each>
</xsl:template>

but havent been able to figure how to do the child parent relationship with [ { and ,.
 
Old February 23rd, 2007, 09:52 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Try this:

Code:
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:param name="input"/>
    <xsl:template match="/book">
        <Item>
            <xsl:for-each select="Item">
                <xsl:for-each select="@PRODUCTID">
                    <PRODUCTID>
                        <xsl:value-of select="xs:short( . )"/>
                    </PRODUCTID>
                </xsl:for-each>
            </xsl:for-each>
            <xsl:for-each select="Item">
                <xsl:for-each select="@ITEM_CREATE">
                    <ITEM_CREATE/>
                </xsl:for-each>
            </xsl:for-each>
            <ITEM_PAY2>
                <x/>
                <y/>
                <xsl:variable name="Vvar16_INPUT">
                    <xsl:choose>
                        <xsl:when test="$input">
                            <xsl:value-of select="$input"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:for-each select="Item">
                                <xsl:for-each select="@ITEM_PAY2">
                                    <xsl:variable name="Vvar19_ITEM_PAY2" select="xs:string( . )"/>
                                    <xsl:value-of select="$Vvar19_ITEM_PAY2"/>
                                </xsl:for-each>
                            </xsl:for-each>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <z>
                    <xsl:value-of select="$Vvar16_INPUT"/>
                </z>
            </ITEM_PAY2>
        </Item>
    </xsl:template>


 
Old February 23rd, 2007, 06:05 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your response, however i am looking to use some sort of a generic code as my actual xml file has a lot of fields with similar rules to be implemented, am trying to use tokenize but it isn't working as expected. Also i wouldn't like to explicity check for the ITEM_PAY2VENDOR_INFO tag as this could be varying in the input xml.


<?xml version="1.0" encoding="UTF-8"?>
<Message externalVersion="2.6" >
  <Header>
          <PartyName>INT</PartyName>
          <ShortName>INT</ShortName>
  </Header>
  <Body>
    <Document subtype="CatEditAdd" type="CatEdit">
              <ItemData>
                <Attribute name="PRODUCTID">
                  <Value>171</Value>
                </Attribute>
                <Attribute name="ITEM_PAY2VENDOR">
                <Value/>
                </Attribute>
                <Attribute name="ITEM_PAY2VENDOR_INFO">
                  <Value>[{CHI_BUY=, COWNERTYPE=-1, CSOURCE_PREFERENCE=2}]</Value>
                </Attribute>
                </ItemData>
    </Document>
  </Body>
 </Message>

xsl code that i have been using
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <ItemData>
            <xsl:for-each select="Message/Body/ItemData/Attribute">
               <xsl:variable name="Value" select="string( . )" />
               <xsl:variable name="Attr" select="string('@name')" />

               <xsl:if test = "Attr = 'ITEM_PAY2VENDOR_INFO'">
                   <xsl:variable name="tokenizedSample" select="tokenize($Value,',')"/>
                   <xsl:for-each select= "tokenizedSample">
                       <xsl:variable name= "tokenizedPairs">
                    <xsl:value-of select= "tokenize('.,', '=')"/>
                </xsl:variable>
                <xsl:for-each select="$tokenizedPairs">
                    <xsl:element name="{$tokenizedPairs[1] }">
                                    <xsl:value-of select="$tokenizedPairs[2]" />
                    </xsl:element>
                </xsl:for-each>
            </xsl:for-each>
               </xsl:if>
            </xsl:for-each>
        </ItemData>
    </xsl:template>
</xsl:stylesheet>

<!--outputExpected.xml>
<?xml version="1.0" encoding="UTF-8"?>
    <ItemData>
        <PRODUCTID>171</PRODUCTID>
        <ITEM_PAY2VENDOR></ITEM_PAY2VENDOR>
        <ITEM_PAY2VENDOR_INFO>
                    <CHI_BUY></CHI_BUY>
                    <COWNERTYPE>-1</COWNERTYPE>
                    <CSOURCE_PREFERENCE>2</CSOURCE_PREFERENCE>
                </ITEM_PAY2VENDOR_INFO>
    </ItemData>





Similar Threads
Thread Thread Starter Forum Replies Last Post
creation of table from xml file s2_saha XML 0 April 27th, 2007 01:14 AM
How to change temporary file creation path shilpa_net2004 General .NET 0 December 14th, 2005 02:53 AM
Dynamic File name creation in subscription. bsuku BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 April 5th, 2005 03:41 PM
Get file creation date shamsad Oracle 0 February 6th, 2004 06:59 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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