如何将ID属性写入另一个节点

时间:2012-01-20 14:04:41

标签: xml xslt xpath

我是XSLT的新手,我不知道如何改变它:

   <GCInitialStep id="id_A" name="INIT"/>
   <GCTransition id="id_B" name="T1"/>
   <GCStep id="id_C" name="A1"/>
   <!-- ... -->
   <GCLink fromObject="id_A" toObject="id_B"/>  
   <GCLink fromObject="id_B" toObject="id_C"/>

   <InitialStep id="id_A" name="INIT"
                parentid=""
                childid="id_B"/>
   <Transition id="id_B" name="T1"
               parentid="id_A"
               childid="id_C"/>
   <Action id="id_C" name="A1"
         parentid="id_B"
         childid=""/>

有可能吗?怎么样?

谢谢:)

1 个答案:

答案 0 :(得分:2)

使用类似的东西:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" />

    <xsl:key name="fromKey" match="GCLink" use="@fromObject"/>
    <xsl:key name="toKey" match="GCLink" use="@toObject"/>

    <xsl:template match="/root">
        <xsl:copy>
            <xsl:apply-templates select="*[not(self::GCLink)]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{substring(name(), 3)}">
            <xsl:copy-of select="@id"/>
            <xsl:copy-of select="@name"/>
            <xsl:attribute name="parentid">
                <xsl:value-of select="key('toKey', @id)/@fromObject"/>
            </xsl:attribute>
            <xsl:attribute name="childid">
                <xsl:value-of select="key('fromKey', @id)/@toObject"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

适用于

<root>
    <GCInitialStep id="id_A" name="INIT"/>
    <GCTransition id="id_B" name="T1"/>
    <GCStep id="id_C" name="A1"/>
    <GCLink fromObject="id_A" toObject="id_B"/>
    <GCLink fromObject="id_B" toObject="id_C"/>
</root>

输出

<root>
  <InitialStep id="id_A" name="INIT" parentid="" childid="id_B" />
  <Transition id="id_B" name="T1" parentid="id_A" childid="id_C" />
  <Step id="id_C" name="A1" parentid="id_B" childid="" />
</root>
相关问题