Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Resolve XML references

  • 10-02-2011 05:30PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 3,265 ✭✭✭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, Registered Users 2 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