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

xml – 在XSLT中的If-then

发布时间:2020-12-16 23:24:31 所属栏目:百科 来源:网络整理
导读:我在从 XML文件中提取一些数据时遇到问题.我仍然有问题要弄清楚如何处理ph:ph:属性如果有多个但是输出看起来像(s_rel0 = 5,par_s_rel0 = 5)而不是(s_rel0 = 5)(par_s_rel0) = 5).也许是if-then案例!? 第二个连接应由flange_b而不是flange_a组成.我在搜索
我在从 XML文件中提取一些数据时遇到问题.我仍然有问题要弄清楚如何处理ph:ph:属性如果有多个但是输出看起来像(s_rel0 = 5,par_s_rel0 = 5)而不是(s_rel0 = 5)(par_s_rel0) = 5).也许是if-then案例!?

第二个连接应由flange_b而不是flange_a组成.我在搜索错误时却找不到它.

你知道我哪里弄错了吗??
谢谢你的帮助.

XML:

<?xml version="1.0" encoding="UTF-8"?>
    <ph:Graphs xmlns:ph="http://www.merge.something.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ph:Graph name="mass_spring_mo">
            <ph:Element id="0" type="Fixed">
                <ph:Port id="1" type="port">
                    <ph:Attribute>
                        <ph:AttributeField name="type" value="string"/>
                        <ph:AttributeField name="name" value="type"/>
                        <ph:AttributeField name="value" value="flange"/>
                    </ph:Attribute>
                </ph:Port>
            </ph:Element>
            <ph:Element id="2" type="Spring">
                <ph:Attribute>
                    <ph:AttributeField name="type" value="int"/>
                    <ph:AttributeField name="name" value="s_rel0"/>
                    <ph:AttributeField name="value" value="5"/>
                </ph:Attribute>
                <ph:Attribute>
                    <ph:AttributeField name="type" value="int"/>
                    <ph:AttributeField name="name" value="par_s_rel0"/>
                    <ph:AttributeField name="value" value="5"/>
                </ph:Attribute>
                <ph:Port id="3" type="port">
                    <ph:Attribute>
                        <ph:AttributeField name="type" value="string"/>
                        <ph:AttributeField name="name" value="type"/>
                        <ph:AttributeField name="value" value="flange_a"/>
                    </ph:Attribute>
                </ph:Port>
                <ph:Port id="4" type="port">
                    <ph:Attribute>
                        <ph:AttributeField name="type" value="string"/>
                        <ph:AttributeField name="name" value="type"/>
                        <ph:AttributeField name="value" value="flange_b"/>
                    </ph:Attribute>
                </ph:Port>
            </ph:Element>
            <ph:Element id="5" type="Mass">
                <ph:Port id="6" type="port">
                    <ph:Attribute>
                        <ph:AttributeField name="type" value="string"/>
                        <ph:AttributeField name="name" value="type"/>
                        <ph:AttributeField name="value" value="flange_a"/>
                    </ph:Attribute>
                </ph:Port>
            </ph:Element>
            <ph:Edge id="7" sourceid="1" targetid="3"/>
            <ph:Edge id="8" sourceid="4" targetid="6"/>
        </ph:Graph>
    </ph:Graphs>

XSLT:

<?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ph="http://www.merge.something.com">

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

        <xsl:template match="/">
            <xsl:apply-templates select="ph:Graphs/ph:Graph"/>
        </xsl:template>

        <xsl:template match="ph:Graph">
            <xsl:text>model</xsl:text>
            <xsl:value-of select="@name"/>
            <xsl:text></xsl:text>
            <xsl:apply-templates select="ph:Element"/>
            <xsl:text>equation</xsl:text>
            <xsl:apply-templates select="ph:Edge"/>
            <xsl:text>end</xsl:text>
            <xsl:value-of select="@name"/>
            <xsl:text>;</xsl:text>
        </xsl:template>

        <xsl:template match="ph:Element">
            <xsl:text> Components.</xsl:text>
            <xsl:value-of select="@type"/>
            <xsl:text > </xsl:text>
            <xsl:value-of select="translate(@type,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
            <xsl:value-of select="@id"/>
            <xsl:apply-templates select="ph:Attribute"/>
            <xsl:text>;</xsl:text>
         </xsl:template>

         <xsl:template match="ph:Element/ph:Attribute">
             <xsl:choose>
                 <xsl:when test="ph:AttributeField[@name = 'type' and @value='int']">
                     <xsl:text>(</xsl:text>
                     <xsl:value-of select="ph:AttributeField[@name = 'name']/@value"/>
                     <xsl:text> = </xsl:text>
                     <xsl:value-of select="ph:AttributeField[@name = 'value']/@value" />
                     <xsl:text>)</xsl:text>
                 </xsl:when>
                 <xsl:when test="ph:AttributeField[@name = 'type' and @value='string']">
                     <xsl:text>(</xsl:text>
                     <xsl:value-of select="ph:AttributeField[@name = 'name']/@value"/>
                     <xsl:text> = '</xsl:text>
                     <xsl:value-of select="ph:AttributeField[@name = 'value']/@value" />
                     <xsl:text>')</xsl:text>
                 </xsl:when>
             </xsl:choose>
         </xsl:template>

         <xsl:template match="ph:Port/ph:Attribute">
             <xsl:if test="ph:AttributeField/@value=type">
                 <xsl:apply-templates select="ph:AttributeField"/>
             </xsl:if>
         </xsl:template>

         <xsl:template match="ph:AttributeField">
         </xsl:template>

         <xsl:template match="ph:Edge">
             <xsl:variable name="sourceid" select="@sourceid"/>
             <xsl:variable name="targetid" select="@targetid"/>
             <xsl:variable name="SourceElement" select="//ph:Element[ph:Port[@id = $sourceid]]"/>
             <xsl:variable name="TargetElement" select="//ph:Element[ph:Port[@id = $targetid]]"/>
             <xsl:text> connect(</xsl:text>
             <xsl:value-of select="translate($SourceElement/@type,'abcdefghijklmnopqrstuvwxyz')" />
             <xsl:value-of select="$SourceElement/@id" />
             <xsl:text>.</xsl:text>
             <xsl:value-of select="$SourceElement/ph:Port/ph:Attribute/ph:AttributeField[@name = 'value']/@value" />
             <xsl:text>,</xsl:text>
             <xsl:value-of select="translate($TargetElement/@type,'abcdefghijklmnopqrstuvwxyz')" />
             <xsl:value-of select="$TargetElement/@id" />
             <xsl:text>.</xsl:text>
             <xsl:value-of select="$TargetElement/ph:Port/ph:Attribute/ph:AttributeField[@name = 'value']/@value" />
             <xsl:text >);</xsl:text>
         </xsl:template>

    </xsl:stylesheet>

禁用输出:

model mass_spring_mo
 Components.Fixed fixed0;
 Components.Spring spring2(s_rel0 = 5,par_s_rel0 = 5);
 Components.Mass mass5;
equation
 connect(fixed0.flange,spring2.flange_a);
 connect(spring2.flange_b,mass5.flange_a);
end mass_spring_mo;

解决方法

以下是xslt中if-else的使用方法

<xsl:choose>
  <xsl:when test="@ordered=1">
    <ol>
      <xsl:apply-templates select="item-ordered" />
    </ol>
  </xsl:when>
  <xsl:otherwise>
    <ul>
      <xsl:apply-templates select="item-unordered" />
    </ul>
  </xsl:otherwise>
</xsl:choose>

(编辑:李大同)

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

    推荐文章
      热点阅读