Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT交换元素值_Xml_Xslt_Gpx - Fatal编程技术网

Xml XSLT交换元素值

Xml XSLT交换元素值,xml,xslt,gpx,Xml,Xslt,Gpx,我试图使用xslt在GPX文件(XML)中交换元素name和cmt的值 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <gpx xmlns="http://www.topografix.com/GPX/1/1" creator="GPS TrackMaker" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schema

我试图使用xslt在GPX文件(XML)中交换元素namecmt的值

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="GPS TrackMaker" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
    <metadata>
        <link href="http://www.trackmaker.com">
            <text>Geo Studio Tecnology Ltd</text>
        </link>
        <time>2012-06-29T21:53:25Z</time>
        <bounds minlat="37.772562" minlon="-124.550080" maxlat="49.323978" maxlon="-122.404474"/>
    </metadata>
    <wpt lat="46.189580000" lon="-123.824460000">
        <ele>0.000000</ele>
        <name>G02010</name>
        <cmt>Columbia River Maritime Museum</cmt>
        <desc>Columbia River Maritime Museum</desc>
        <sym>Museum</sym>
    </wpt>
    <wpt lat="46.189010000" lon="-123.824370000">
        <ele>0.000000</ele>
        <name>G02020</name>
        <cmt>Marine Dr/17th St</cmt>
        <desc>Marine Dr/17th St</desc>
        <sym>Waypoint</sym>
    </wpt>
</gpx>

地理工作室技术有限公司
2012-06-29T21:53:25Z
0
G02010
哥伦比亚河海事博物馆
哥伦比亚河海事博物馆
博物馆
0
G02020
海事博士/第17街
海事博士/第17街
航路点
改造后:

...
<name>Columbia River Maritime Museum</name>
<cmt>G02010</cmt>
...
。。。
哥伦比亚河海事博物馆
G02010
...
有人能为这个显示正确的xslt吗?
谢谢。

使用标识转换并覆盖这些元素的值:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:gpx="http://www.topografix.com/GPX/1/1">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="gpx:name/text()">
        <xsl:value-of select="../../gpx:cmt/text()"/>
    </xsl:template>
    <xsl:template match="gpx:cmt/text()">
        <xsl:value-of select="../../gpx:name/text()"/>
    </xsl:template>
</xsl:stylesheet>
这是一个稍微更一般的解决方案,但考虑到您提供的输入,第一个就足够了

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:gpx="http://www.topografix.com/GPX/1/1">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="gpx:name">
        <xsl:copy>
            <xsl:apply-templates select="@*|../gpx:cmt/text()"/>
        </xsl:copy>
    </xsl:template>
        <xsl:template match="gpx:cmt">
        <xsl:copy>
            <xsl:apply-templates select="@*|../gpx:name/text()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>