Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Resolve XML references

Options
  • 10-02-2011 5:30pm
    #1
    Registered Users Posts: 495 ✭✭


    hi
    I have a large XML file, filled with elements like this
    <myItems>
    	<items>
    		<item href="#id0"></item>
    		<item href="#id1"></item>
    	</items>
    	<multiRef id="id0">
    		<description>id0 description</description>
    		<title>id0 title</title>
    	</multiRef>
    	<multiRef id="id1">
    		<description>id1 description</description>
    		<title>id1 title</title>
    	</multiRef>
    </myItems>
    

    The XML itself is valid, but I'm looking for a tool that will read in the source XML and resolve the references to produce an output like this.
    <myItems>
    	<items>
    		<item>
    			<description>id0 description</description>
    			<title>id0 title</title>
    		</item>
    		<item>
    			<description>id1 description</description>
    			<title>id1 title</title>
    		</item>
    	</items>
    </myItems>
    

    I could do it manually, but there are literally hundreds of item references, and some child items have references in them.

    I was thinking possible an XSLT stylesheet might do it?


Comments

  • Registered Users Posts: 3,282 ✭✭✭BlackWizard


    I would just write some C# or other language to just parse and recreate the XML.

    Just read every multiref and get the description and title, then write it to a a file in the format you want.


  • Registered Users Posts: 495 ✭✭tetsujin1979


    yeah, that's probably what I'll end up doing

    just wondering if there's a simpler way of doing it


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    <?xml version="1.0" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/myItems">
        &lt;myItems&gt;
        	<xsl:for-each select="multiRef">
    			<ul style="list-style:none;">
    			<li>&lt;item&gt;
    				<ul style="list-style:none;">
    					<li>&lt;description&gt;
    						<ul style="list-style:none;">
    							<li><xsl:value-of select="description" /></li>
    						</ul>&lt;/description&gt;
    					</li>
    					<li>&lt;title&gt;
    						<ul style="list-style:none;">
    							<li><xsl:value-of select="title" /></li>
    						</ul>&lt;/title&gt;
    					</li>
    				</ul>&lt;/item&gt;
    			</li>
    			</ul>
    		</xsl:for-each>
        &lt;/myItems&gt;
    </xsl:template>
    </xsl:stylesheet>
    
    

    Save the above XSLT file and add it as the stylesheet for your XML document. Looking at your requirements, you don't seem to need/use the item tag values at all so I've ignored them. The above will generate a html file with the necessary tags. Just copy and paste.

    -RD


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    <?xml version="1.0" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/myItems">
        &lt;myItems&gt;&lt;items&gt;
        	<xsl:for-each select="multiRef">
    			<ul style="list-style:none;">
    			<li>&lt;item&gt;
    				<ul style="list-style:none;">
    					<li>&lt;description&gt;
    						<ul style="list-style:none;">
    							<li><xsl:value-of select="description" /></li>
    						</ul>&lt;/description&gt;
    					</li>
    					<li>&lt;title&gt;
    						<ul style="list-style:none;">
    							<li><xsl:value-of select="title" /></li>
    						</ul>&lt;/title&gt;
    					</li>
    				</ul>&lt;/item&gt;
    			</li>
    			</ul>
    		</xsl:for-each>
        &lt;/items&gt;&lt;/myItems&gt;
    </xsl:template>
    </xsl:stylesheet>
    
    

    Slight change to code to allow for extra tag


Advertisement