#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Insert data from one XML file into another, existing XML document with XSLT


    Hi,

    I have two source XML files and I want to take some of the contents of one and insert it into the other.

    Source file 1 (metadatasource.xml):

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <metadata>
    <pages>300</pages>
    <publishdate>1900</publishdate>
    </metadata>

    My other file is (booksource.xml):

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <book>
    <metadata>
    <title>Tom Sawyer</title>
    <author>Mark Twain</author>
    </metadata>
    <content>
    <page>1</page>
    <page>2</page>
    <page>3</page>
    </content>
    </book>

    I want to take the content in the <metadata> of metadatasource.xml and add it to the <metadata> of booksource.xml, while maintaining the integrity of the rest of the booksource.xml file (I have edited for brevity sake).

    I am able to get the content from metadatsource.xml and dump into a new file, but am unable to add into booksource.xml.

    Here is my xsl thus far:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet>

    <xsl:template match="/">
    <pages><xsl:value-of select="document('metadatasource.xml')/metadata/pages"/></pages>
    <publishdate><xsl:value-of select="document('metadatasource.xml')/metadata/publishdate"/></publishdate>
    </xsl:template>
    </xsl:stylesheet>

    thoughts?

    thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    When you use browser the browser display
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output indent="yes" method="xml"/>
    	<xsl:template match="/">
    		<root>
    			<xsl:apply-templates select="book"/>
    		</root>
    	</xsl:template>
    	<xsl:template match="book">
    		<xsl:apply-templates select="metadata"/>
    		<xsl:apply-templates select="content"/>
    	</xsl:template>
    	<xsl:template match="metadata">
    		<metadata>
    			<title>
    				<xsl:value-of select="title"/>
    			</title>
    			<auther>
    				<xsl:value-of select="author"/>
    			</auther>
    			<xsl:variable name="data" select="document('metadatasource.xml')/metadata"/>
    			<page>
    				<xsl:value-of select="$data/pages"/>
    			</page>
    			<publishdate>
    				<xsl:value-of select="$data/publishdate"/>
    			</publishdate>
    		</metadata>
    	</xsl:template>
    	<xsl:template match="content">
    		<content>
    			<xsl:apply-templates select="page"/>
    		</content>
    	</xsl:template>
    	<xsl:template match="page">
    		<page>
    			<xsl:value-of select="."/>
    		</page>
    	</xsl:template>
    </xsl:stylesheet>
    this

    Code:
    <?xml version='1.0' ?>
    <root>
      <metadata>
        <title>Tom Sawyer</title>
        <auther>Mark Twain</auther>
        <page>300</page>
        <publishdate>1900</publishdate>
      </metadata>
      <content>
        <page>1</page>
        <page>2</page>
        <page>3</page>
      </content>
    </root>
    but you a parser
    then

    parser xsl-file xml-file output-file

    output-file is same name as xml file
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0
    edit: removed
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    can you display booksource.xml
    is diffclut or ritle
    where are the attributes

    Code:
    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <book>
    	<metadata>
    		<title>Tom Sawyer</title>
    		<author>Mark Twain</author>
    	</metadata>
    	<content attr1="attr1" href="attr2">
    		<item attr1="attr1" href="attr2"></item>
    		<page>1</page>
    		<page>2</page>
    		<page>3</page>
    	</content>
    </book>
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    xml


    Okay? can you display booksource.xml

IMN logo majestic logo threadwatch logo seochat tools logo