加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用XSLT的XML父子连接

发布时间:2020-12-16 22:52:04 所属栏目:百科 来源:网络整理
导读:INPUT XML: ?xml version="1.0" encoding="ISO-8859-1"?systemmap name="Map" map name="Map1" colorMap1 is red colored/color shapeMap1 is square shaped/shape /map map name="Map2" colorMap2 is green colored/color map name="Tap1" colorTap1 is ye
INPUT XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<system>
<map name="Map">
    <map name="Map1">
        <color>Map1 is red colored</color>
        <shape>Map1 is square shaped</shape>
    </map>
    <map name="Map2">
        <color>Map2 is green colored</color>
        <map name="Tap1">
            <color>Tap1 is yellow colored</color>
            <speed>Tap1 is very fast</speed>
        </map>
        <map name="Tap2">
            <speed>Tap2 is very slow</speed>
        </map>
    </map>
</map>
</system>

期望的输出:

<?xml version="1.0" encoding="ISO-8859-1"?>
<system>
<map name="Map+Map1">
    <color>Map1 is red colored</color>
    <shape>Map1 is square shaped</shape>
</map>
<map name="Map+Map2">
    <color>Map2 is green colored</color>
</map>
<map name="Map+Map2+Tap1">
    <color>Tap1 is yellow colored</color>
    <speed>Tap1 is very fast</speed>
</map>
<map name="Map+Map2+Tap2">
    <speed>Tap2 is very slow</speed>
</map>

当前输出:

<system>
    <map name="Map1"/>
    <map name="Map1"/>
    <map name="Map2"/>  
    <map name="Map2.Tap1"/>
    <map name="Map2.Tap1"/>
    <map name="Map2.Tap2"/>
</system>

当前的XSL样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="map[@name]">
    <xsl:apply-templates mode="next">
       <xsl:with-param name="name" select="''" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="map[@name]" mode="next">
    <xsl:param name="name" />
    <xsl:apply-templates mode="next">
    <xsl:with-param name="name" select="concat($name,'.',@name)" />
    </xsl:apply-templates>
</xsl:template>


<xsl:template match="*[not(*)]" mode="next">
    <xsl:param name="name" />
    <xsl:element name="map">
    <xsl:attribute name="name">
        <xsl:variable name="nameLength" select="string-length($name)-1"/>
            <xsl:value-of select="substring($name,2,$nameLength)"/>
    </xsl:attribute>
    </xsl:element>
</xsl:template>

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

</xsl:stylesheet>

我只是XSL的初学者.我在网上提到了很多,并试图解决这个问题.
但我失败了.有人请帮助我获得正确的xsl代码以实现相同的目标.
请.

解决方法

你可以尝试这样的事情:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="map" mode="concat" >
        <xsl:param name="names" select="''" />
        <xsl:variable name="newName" select="concat($names,@name)" />
        <xsl:copy>
            <xsl:attribute name ="name">
                <xsl:value-of select="$newName"/>
            </xsl:attribute>
            <xsl:copy-of select="*[name() != 'map']"/>
        </xsl:copy>
        <xsl:apply-templates select="map" mode ="concat">
            <xsl:with-param name="names" select="concat($newName,'+')" />
        </xsl:apply-templates>

    </xsl:template>

    <xsl:template match="*" >
        <xsl:copy>
            <xsl:apply-templates select="map" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/*/map">
        <xsl:apply-templates select="map" mode="concat">
            <xsl:with-param name ="names" select="concat(@name,'+')" />
        </xsl:apply-templates>  
    </xsl:template>

</xsl:stylesheet>

在这个小例子中,模式我不是必需的,但如果xlst文件增长则会有用.
Otuput:

<?xml version="1.0"?>
<system>
  <map name="Map+Map1">
    <color>Map1 is red colored</color>
    <shape>Map1 is square shaped</shape>
  </map>
  <map name="Map+Map2">
    <color>Map2 is green colored</color>
  </map>
  <map name="Map+Map2+Tap1">
    <color>Tap1 is yellow colored</color>
    <speed>Tap1 is very fast</speed>
  </map>
  <map name="Map+Map2+Tap2">
    <speed>Tap2 is very slow</speed>
  </map>
</system>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读