Menu

Using the XML ed Move Command

Help
Andy Kerr
2009-12-18
2013-03-13
  • Andy Kerr

    Andy Kerr - 2009-12-18

    Brand new to XML Starlet and am having problems even coming up with the syntax for a 'move' command

    Basically have the file structure below (only showing relevant elements)

        <Address>
        <PostCode>E5 0QR</PostCode>
        <BS7666Address>
        <StreetNo> 130</StreetNo>
        <Road>Lower Clapton Road</Road>
        <Town>London</Town>
        <Town>London</Town>
        </BS7666Address>
        </Address>

    I need to move the PostCode tags (and value) within the BS7666Address tag (so it becomes a child of that element) as below…

        <Address>
        <BS7666Address>
        <StreetNo>28</StreetNo>
        <Road>Springfield Avenue</Road>
        <Neighbourhood>Kempston</Neighbourhood>
        <Town>Bedford</Town>
        <County>Bedfordshire</County>
        <Postcode>MK42 8HU</Postcode>
        </BS7666Address>
        </Address>

    Any help with syntax for XML Starlet command would be greatly appreciated.

     
  • grobbla

    grobbla - 2009-12-28

    This seems to work:

    xml.exe  ed -m //PostCode //BS7666Address m.xml

     
  • Andy Kerr

    Andy Kerr - 2009-12-28

    grobbla,

    Thanks for the response, probably should have mentioned that there are multiple records in the file (I tried something very similar to what you indicated above).

    Using the syntax above returns…

    destination nodeset does not contain one node (node count is 0)

    Any ideas?

    Thanks

     
  • grobbla

    grobbla - 2010-01-05

    Hello,

    another path would be to use a xsl file. You need to add a root node named i.e. "book" to the input list, for output formating. Just look at the files:

    Example input m.xml:

    <book>
    <Address>
    <PostCode>E5 0QR-1</PostCode>
    <BS7666Address>
    <StreetNo> 130</StreetNo>
    <Road>Lower Clapton Road</Road>
    <Town>London</Town>
    <Town>London</Town>
    </BS7666Address>
    </Address>
    <Address>
    <PostCode>E5 0QR-2</PostCode>
    <BS7666Address>
    <StreetNo> 130</StreetNo>
    <Road>Lower Clapton Road</Road>
    <Town>London</Town>
    <Town>London</Town>
    </BS7666Address>
    </Address>
    </book>


    Stylesheet m.xsl:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/book">
    <xsl:element name="book">
    <xsl:for-each select="Address">
    <xsl:element name="Address">
    <xsl:element name="BS7666Address">
    <xsl:copy-of select="PostCode"/>
    <xsl:for-each select="BS7666Address">
    <xsl:copy-of select="*"/>
    </xsl:for-each>
    </xsl:element>
    </xsl:element>
    </xsl:for-each>
    </xsl:element>
      </xsl:template>
    </xsl:stylesheet>


    Xml command:

    xml.exe tr m.xsl m.xml | xml.exe fo

    Expected result:

    <?xml version="1.0"?>
    <book>
      <Address>
    <BS7666Address>
      <PostCode>E5 0QR-1</PostCode>
      <StreetNo> 130</StreetNo>
      <Road>Lower Clapton Road</Road>
      <Town>London</Town>
      <Town>London</Town>
    </BS7666Address>
      </Address>
      <Address>
    <BS7666Address>
      <PostCode>E5 0QR-2</PostCode>
      <StreetNo> 130</StreetNo>
      <Road>Lower Clapton Road</Road>
      <Town>London</Town>
      <Town>London</Town>
    </BS7666Address>
      </Address>
    </book>


    regards, fred

     
  • grobbla

    grobbla - 2010-01-06

    I just realized, two closing tags are missing in the post above.

    The closing tags "book" are missing from the example input file, and the expected result.

     
  • grobbla

    grobbla - 2010-01-06

    Argh, and the closing "xsl:stylesheet" tag in the xsl file is missing, too …

     
  • Andy Kerr

    Andy Kerr - 2010-01-07

    Fred,

    Thanks for all your help mate - tried original xsl and it failed, but amendments seemed have sorted it.

    Much appreciated

    Andy

     
  • grobbla

    grobbla - 2010-01-08

    No problem, you are welcome.

    To my surprise, the closing tags are back in the post above, sourceforge must be changing the forums somehow.

    regards, fred

     

Log in to post a comment.

MongoDB Logo MongoDB