<?xml version="1.0" encoding="utf-8" ?>
<XMLScreen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <HMIWindow Type="Window" Name="Window_1">
    <Attribute Name="BackColor" Value="ffffffff" />
    <Attribute Name="ImageURI" Value="" />
    <Attribute Name="Enabled" Value="True" />
    <Attribute Name="Visible" Value="True" />
  </HMIWindow>
  <Control Type="LINE" Name="LINE_1">
    <Attribute Name="BackColor" Value="ffffffff" />
    <Attribute Name="ImageURI" Value="" />
    <Attribute Name="Enabled" Value="True" />
    <Attribute Name="Visible" Value="True" />
  </Control>
</XMLScreen>
要通过XSLT 生成新的xml文档如下效果:
<?xml version="1.0" encoding="utf-8" ?>
<XMLScreen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <HMIWindow Type="Window" Name="Window_1" CType=“CWND” CName=“CWND_1”>
    <Attribute Name="BackColor" Value="ffffffff" />
    <Attribute Name="ImageURI" Value="" />
    <Attribute Name="Enabled" Value="True" />
    <Attribute Name="Visible" Value="True" />
  </HMIWindow>
  <Control Type="LINE" Name="LINE_1" CType=“CLine” CName=“Line_1”>
    <Attribute Name="BackColor" Value="ffffffff" />
    <Attribute Name="ImageURI" Value="" />
    <Attribute Name="Enabled" Value="True" />
    <Attribute Name="Visible" Value="True" />
  </Control>
</XMLScreen>希望是根据Type属性来判断 它是窗口还是控件,然后再插入自己想要的属性进去。

解决方案 »

  1.   

    还是别用XSLT啦,麻烦死了,还不如直接生成XML文件
      

  2.   


    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="*">
    <xsl:copy>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="//HMIWindow">
    <xsl:for-each select="//HMIWindow">
    <xsl:element name="HMIWindow">
    <xsl:attribute name="Type"><xsl:value-of select="@Type"/></xsl:attribute>
    <xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
    <xsl:attribute name="CType">CWND</xsl:attribute>
    <xsl:attribute name="CName">CWND_1</xsl:attribute>
    <xsl:apply-templates select="Attribute"/>
    </xsl:element>
    </xsl:for-each>
    </xsl:template> <xsl:template match="//Control">
    <xsl:for-each select="//Control">
    <xsl:element name="Control">
    <xsl:attribute name="Type"><xsl:value-of select="@Type"/></xsl:attribute>
    <xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
    <xsl:attribute name="CType">CLine</xsl:attribute>
    <xsl:attribute name="CName">Line_1</xsl:attribute>
    <xsl:apply-templates select="Attribute"/>
    </xsl:element>
    </xsl:for-each>
    </xsl:template> <xsl:template match="Attribute">
    <xsl:element name="Attribute">
    <xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
    <xsl:attribute name="Value"><xsl:value-of select="@Value"/></xsl:attribute>
    </xsl:element>
    </xsl:template></xsl:stylesheet>
      

  3.   

    楼上的可以觉解部分问题,大问题还没有解决
    如下::
    <XMLScreen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <--!因为是在一个窗口里添加很多控件,因此有很多未知性,所以生成xml的时候要根据Type先判断下,而且Name属性也要实现后缀递增-->
      <HMIWindow Type="Window" Name="Window_1"> 
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </HMIWindow> 
      <Control Type="LINE" Name="LINE_1"> 
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </Control>
      <Control Type="LINE" Name="LINE_2"> 
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </Control> 
    <--!可能还有LINE_3 、LINE_4呀,不确定有多少-->
    <Control Type="Circle" Name="Circle_1"> 
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </Control>
    <--!可能还有GradientTriangle(矩形) 、扇形、button等等,不确定有多少种控件,要根据判断是否有无来生成新的xml-->
    </XMLScreen> 
    生成效果为:
    <?xml version="1.0" encoding="utf-8" ?> 
    <XMLScreen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <HMIWindow Type="Window" Name="Window_1" CType="CWindow" CName="CWindow_1"> 
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </HMIWindow> 
      <Control Type="LINE" Name="LINE_1" CType="CLine" CName="CLine_1">
    <--!可能还有第二条线、第三条线、......包含未确定性,也要根据判断生成出来--> 
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </Control>
      <Control Type="LINE" Name="LINE_2" CType="CLine" CName="CLine_2"> 
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </Control> 
    <Control Type="Circle" Name="Circle_1" CType="CCircle" CName="CCircle_1"> 
    <--!可能还有矩形、button 、checkbox等控件也要根据判断有无这种控件来生成新的XML,生成样式与上面一样-->
        <Attribute Name="BackColor" Value="ffffffff" /> 
        <Attribute Name="ImageURI" Value="" /> 
        <Attribute Name="Enabled" Value="True" /> 
        <Attribute Name="Visible" Value="True" /> 
      </Control>
    </XMLScreen>