Xml 在XSLT中创建动态变量名,或者如何解决此问题?

Xml 在XSLT中创建动态变量名,或者如何解决此问题?,xml,xslt,text,xslt-1.0,Xml,Xslt,Text,Xslt 1.0,从该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=

从该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: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:Element>
    <ph:Edge id="17" sourceid="1" targetid="3"/>
</ph:Graph>
</ph:Graphs>
我的问题是获取应该连接的元素的对应名称和类型。我试图生成一个动态名称的变量,比如id=1,但它不起作用。 也许有一个更简单的解决方案来引用元素的属性

如果有人能给我一个提示,我将非常感激

谢谢, 再见,米歇尔这里有一个模板。 您可以看到,我对行尾使用了与您不同的方法。 此外,通过在ph:Edge模板中定义SourceElement和TargetElement变量来完成回溯。我只选择ph:Element元素,该元素具有与ph:Edge的sourceid属性匹配的id的对应ph:Port子元素。一旦您能够识别它们,引用它们的属性就很容易了

<?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>&#10;</xsl:text>
        <xsl:apply-templates select="ph:Element"/>
        <xsl:text>equation&#10;</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>;&#10;</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', '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', '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 >);&#10;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

型号
;
方程
;
结束;
组件。
;

( = )
( = '')
连接(
.
,
.
);


你什么都没解释。什么是“应该连接的元素”?这个XML文档的语义是什么?一点也不清楚。就目前的形式而言,这不是一个定义明确的问题。请编辑/改进。好的!我认为将标识符结构(使用
翻译(…)
)提取到一个单独的模板中(将适当的
ph:Element
节点作为参数传递),甚至可能使用变量
$uppercase
$lowercase
以提高可读性……非常感谢您的回答,它可以找到。我还有一个问题:如果有多个元素,但输出应该是
(s_rel0=10,var2=12)
,如何处理
ph:Element的
。如果还有更多的
ph:Port
属性呢!?更多ph:端口应该已经被处理,因为它与端口的id属性匹配。至于multipleph:Element/ph:Attribute,您必须将一些内容移动到ph:Element模板中,如(and)。我可能还会执行foreach ph:属性,以便插入逗号。
model mass_spring_mo
 Components.Fixed fixed1;
 Components.Spring spring1(s_rel0 = 10);
equation  
 connect(fixed1.flange,spring1.flange_a);
end mass_spring_mo;
<?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>&#10;</xsl:text>
        <xsl:apply-templates select="ph:Element"/>
        <xsl:text>equation&#10;</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>;&#10;</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', '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', '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 >);&#10;</xsl:text>
    </xsl:template>

</xsl:stylesheet>