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 March 23rd, 2007, 05:42 PM
Authorized User
 
Join Date: Mar 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to XML transform (Simplified)

I have following XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
<ONIXMessage>
  <Header>
    <FromEANNumber>5013546111661</FromEANNumber>
    <FromCompany>NielsenBookData</FromCompany>
    <FromPerson>Angela Kendall</FromPerson>
    <FromEmail>[email protected]</FromEmail>
    <SentDate>20061213</SentDate>
    <DefaultLanguageOfText>eng</DefaultLanguageOfText>
  </Header>
  <Product>
    <RecordReference>9780643091610</RecordReference>
    <NotificationType>03</NotificationType>
    <RecordSourceType>04</RecordSourceType>
    <RecordSourceName>Nielsen BookData</RecordSourceName>
    <RecordSourceIdentifierType>02</RecordSourceIdentifierType>
    <RecordSourceIdentifier>NBD</RecordSourceIdentifier>
    <ProductIdentifier>
      <ProductIDType>15</ProductIDType>
      <IDValue>9780643091610</IDValue>
    </ProductIdentifier>
   </Product>
</ONIXMessage>

and have following XSL
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:MyDateTime="urn:MyDateTime" exclude-result-prefixes="MyDateTime">

    <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" omit-xml-declaration="no" indent="yes"/>
    <xsl:output doctype-system="http://www.editeur.org/onix/2.1/reference/onix-international.dtd"/>

    <xsl:template match="/">
        <xsl:comment>Transformed by ONIX Loader to make this valid XML file on <xsl:value-of select="MyDateTime:ToString()"/></xsl:comment>

        <ONIXMessage>
            <Header>
                <xsl:apply-templates select="ONIXMessage/Header/node()"/>
            </Header>

            <Product>
                <xsl:apply-templates select="ONIXMessage/Product/RecordReference"/>
                <xsl:apply-templates select="ONIXMessage/Product/NotificationType"/>
                <xsl:apply-templates select="ONIXMessage/Product/RecordSourceType"/>
                <xsl:apply-templates select="ONIXMessage/Product/RecordSourceIdentifierType"/>
                <xsl:apply-templates select="ONIXMessage/Product/RecordSourceIdentifier"/>
                <xsl:apply-templates select="ONIXMessage/Product/RecordSourceName"/>

                <xsl:apply-templates select="ONIXMessage/Product/node()"/>
            </Product>
        </ONIXMessage>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Output is
<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
<ONIXMessage>
  <Header>
    <FromEANNumber>5013546111661</FromEANNumber>
    <FromCompany>NielsenBookData</FromCompany>
    <FromPerson>Angela Kendall</FromPerson>
    <FromEmail>[email protected]</FromEmail>
    <SentDate>20061213</SentDate>
    <DefaultLanguageOfText>eng</DefaultLanguageOfText>
  </Header>
  <Product>
    <RecordReference>9780643091610</RecordReference>
    <NotificationType>03</NotificationType>
    <RecordSourceType>04</RecordSourceType>
    <RecordSourceIdentifierType>02</RecordSourceIdentifierType>
    <RecordSourceIdentifier>NBD</RecordSourceIdentifier>
    <RecordSourceName>Nielsen BookData</RecordSourceName>

    <RecordReference>9780643091610</RecordReference>
    <NotificationType>03</NotificationType>
    <RecordSourceType>04</RecordSourceType>
    <RecordSourceName>Nielsen BookData</RecordSourceName>
    <RecordSourceIdentifierType>02</RecordSourceIdentifierType>
    <RecordSourceIdentifier>NBD</RecordSourceIdentifier>

    <ProductIdentifier>
      <ProductIDType refname="ProductIDType" shortname="b221">15</ProductIDType>
      <IDValue refname="IDValue" shortname="b244">9780643091610</IDValue>
    </ProductIdentifier>
    <ProductForm>BB</ProductForm>
 </Product>
</ONIXMessage>

I just do not want tags coming in Red color (do not want to repeat again)
 
Old March 24th, 2007, 07: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

You only need to use apply-templates.. like this:


<Product>
    <xsl:apply-templates/>
</Product>

Code:
<xsl:template match="/">
        <xsl:comment>Transformed by ONIX Loader to make this valid XML file on</xsl:comment>

        <ONIXMessage>
            <Header>
                <xsl:apply-templates select="ONIXMessage/Header/node()"/>
            </Header>
            <Product>
                <xsl:apply-templates/>
            </Product>
        </ONIXMessage>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="node()"/>
        </xsl:copy>
    </xsl:template>


 
Old March 24th, 2007, 07:57 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

As a note, the reason you got all the items in red was becasue of this line:

    <xsl:apply-templates select="ONIXMessage/Product/node()"/>

just remove it







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
how to transform a xml to another xml spring152103 XSLT 3 September 22nd, 2008 07:45 PM
XML to XML transform nmahesh567 XSLT 8 March 25th, 2007 06:36 PM
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.