BernieSF
BernieSF

Reputation: 1828

Move and rename KML node using XSLT

I have the following KML (XML) document:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
  <Folder>
    <Placemark>
      <name>my name</name>
      <styleUrl>#my url</styleUrl>
      <ExtendedData>
         <Data name="firstID">
           <value>01234567</value>
         </Data>
         <Data name="secondID">
             <value/>
         </Data>
      </ExtendedData>
      <description>
           long description here
      </description>
      <Point>
         <coordinates>-1.1111,2.22222</coordinates>
      </Point>
      <address>my address</address>
    </Placemark>
  </Folder>
</Document>
</kml>

I have achieved to move the node 'address' into 'ExtendedData', and removed the node 'data name="secondId"', now I need to rename the tag 'address' to 'Data name="Address"' and enclose the address value into 'value' tags, resulting in something like this:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
  <Folder>
    <Placemark>
      <name>my name</name>
      <styleUrl>#my url</styleUrl>
      <ExtendedData>
         <Data name="Address">
           <value>my address</value>
         </Data>
         <Data name="firstID">
           <value>01234567</value>
         </Data>
      </ExtendedData>
      <description>
           long description here
      </description>
      <Point>
         <coordinates>-1.1111,2.22222</coordinates>
      </Point>
    </Placemark>
  </Folder>
</Document>
</kml>

This is my XSLT so far (moves the address and removes the secondID):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:kml="http://www.opengis.net/kml/2.2">
<xsl:output method= "xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="kml:address"/>

<xsl:template match="kml:ExtendedData">
   <xsl:copy>
     <xsl:copy-of select="../kml:address"/>
     <xsl:apply-templates/>
   </xsl:copy>
</xsl:template>

<xsl:template match="kml:Data[@name='secondID']" />

</xsl:stylesheet>

To rename, I tried this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:kml="http://www.opengis.net/kml/2.2">
<xsl:output method= "xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="kml:address">
    <Data name="Address">
       <value>
        <xsl:apply-templates select="@* | node()"/>
       </value>
    </Data>
</xsl:template>

<xsl:template match="kml:ExtendedData">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:copy-of select="../kml:Data[@name='Address']"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="kml:Data[@name='secondID']" />

</xsl:stylesheet>

This renames the address, but adds the attribute 'xmlns=""' to the tag, and doesn't move the newly renamed tag inside 'ExtendedData'

Any ideas? I'm new to XLST. Thanks in advance

Upvotes: 1

Views: 61

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

How about:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns="http://www.opengis.net/kml/2.2" 
exclude-result-prefixes="kml">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="kml:ExtendedData">
    <xsl:copy>
        <!-- move address to here -->
        <Data name="Address">
            <value>
                <xsl:value-of select="../kml:address"/>
            </value>
        </Data>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<!-- remove address at original position -->
<xsl:template match="kml:address"/>

</xsl:stylesheet>

Note the added namespace declaration at the xsl:stylesheet start-tag.

Upvotes: 3

Related Questions