开发者

Create dynamic variable name in XSLT or how can you solve this problem?

开发者 https://www.devze.com 2023-03-02 20:58 出处:网络
I have a problem extrating some data out of this XML file: <?xml version=\"1.0\" encoding=\"UTF-8\"?>

I have a problem extrating some data out of this XML file:

<?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>

Therefore I created this XSLT file:

<?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:Eleme开发者_StackOverflow社区nt"/>
        <xsl:text>
        </xsl:text> 
        <xsl:text>equation</xsl:text>
        <xsl:text>
        </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:variable name="type" select="@type"/>
        <xsl:variable name="id" select="@id"/>

        <xsl:text>Components.</xsl:text>
        <xsl:value-of select="@type"/>
        <xsl:text > </xsl:text>
        <xsl:value-of select="@type"/><xsl:value-of select="@id"/>
            <xsl:apply-templates select="ph:Port/ph:Attribute"/>
    </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:text>connect(</xsl:text>
         <xsl:text >);</xsl:text>
         <xsl:text>
        </xsl:text>
    </xsl:template>

</xsl:stylesheet>

The output should look like that:

model mass_spring_mo
 Components.Fixed fixed1;
 Components.Spring spring1(s_rel0 = 10);
equation  
 connect(fixed1.flange,spring1.flange_a);
end mass_spring_mo;

My problem is to get the correspondent name and type of the elements which should be connected. I tried to generate variable with an dynamic name like the id=1 but it doesn't work. Maybe there is an easier solution to refer to the properties of the element!?

If anyone could give me a hint, I would be very grateful.

Thanks, Bye Michele


Here is a template. You can see that I used a different method for line endings than you did. Also the look back is done by defining the SourceElement and TargetElement variables in the ph:Edge template. I just select the ph:Element element with the corresponding ph:Port child with an id that matches the sourceid attribute of the ph:Edge. Once you can identify those, referencing their properties is easy enough.

<?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>
0

精彩评论

暂无评论...
验证码 换一张
取 消