为每个或另一个xslt元素使用额外的xsl?

时间:2017-10-06 09:40:21

标签: xml xslt

我正在研究使用Cooktop 2.5制作XSLT 1.0的学士学位,以便将XML文件转换为另一个。现在,我再次陷入转换这种形式的XML,因为我是初学者使用XSLT。我几次问过另一个问题的解决方案,这对我帮助很大。所以我试着在这里再次找到解决方案。我有一个像这样的XML(实际上有更多的SignalLine和InstrumentLoop元素):

<PlantModel>
     <SignalLine>
         <Connection FromID="P11108P11104" ToID="PIT-108"/>
    </SignalLine>
    <SignalLine>
         <Connection FromID="PIT-108" ToID="PI-108"/>
    </SignalLine>
    <SignalLine>
        <Connection FromID="PI-108" ToID="PDY-110"/>
    </SignalLine>
    <SignalLine>
         <Connection FromID="PI-108" ToID="PDY-111"/>
    </SignalLine>
    <SignalLine>
         <Connection FromID="P11107P11104" ToID="PIT-107"/>
    </SignalLine>
    <SignalLine>
         <Connection FromID="PIT-107" ToID="PI-107"/>
    </SignalLine>
    <SignalLine>
         <Connection FromID="PI-107" ToID="PDY-111"/>
    </SignalLine>
    <SignalLine>
        <Connection FromID="PDY-111" ToID="PDI-111"/>
    </SignalLine>
    <SignalLine>
        <Connection FromID="P11107P11104" ToID="PIT-109"/>
    </SignalLine>
    <SignalLine>
         <Connection FromID="PIT-109" ToID="PI-109"/>
    </SignalLine>
    <SignalLine>
        <Connection FromID="PI-109" ToID="PDY-110"/>
    </SignalLine>
    <SignalLine>
        <Connection FromID="PDY-110" ToID="PDI-110"/>
    </SignalLine>
    <SignalLine>
        <Connection FromID="PIT-113" ToID="FV-105"/>
    </SignalLine>
    <InstrumentLoop>
        <Association TagName="PIT-107"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PI-107"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PIT-108"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PI-108"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PIT-109"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PI-109"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PDY-110"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PDI-110"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PDY-111"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PDI-111"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PI-113"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="PIT-113"/>
    </InstrumentLoop>
    <InstrumentLoop>
        <Association TagName="FV-105"/>
    </InstrumentLoop>
</PlantModel>

我想将信号线元素的信息作为块。因此,我想从P&amp; ID(管道和仪器图)XML中对仪器回路进行分类。我从第一个字母和@TagName和SignalLine / Connection元素的数量对它进行分类。如果来自一个SignalLine元素的@FromID或@ToID的值与来自另一个SignalLine元素的@FromID或@ToID的值相等,并且值@FromID或@ToID,则该另一个元素等于另一个元素的值@FromID或@ToID SignalLine元素,它说它们是一个仪器循环。所以,从样本中我想要XML输出如下:

<Project>
<Models>
    <Block Id="P-107P-108P-109P-110P-111">
        <ModelChildren>
            <Block Id="PIT-107"/>
            <Block Id="PI-107"/>
            <Block Id="PIT-108"/>
            <Block Id="PI-108"/>
            <Block Id="PIT-109"/>
            <Block Id="PI-109"/>
            <Block Id="PDY-110"/>
            <Block Id="PDI-110"/>
            <Block Id="PDY-111"/>
            <Block Id="PDI-111"/>
        </ModelChildren>
    </Block>
</Models>
</Project>

我成功地将一个乐器循环分类,如果它只有相同的第一个字母和@TagName的数字,并且它是否有2个不同的首字母和@TagName的数字。现在,我遇到了问题,因为一个仪器循环有4个不同数量的@TagName。我试过像这样的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Project>
<Models>
    <xsl:for-each select = "/PlantModel/SignalLine[(string-length(Connection/@FromID) &lt; 8) and (string-length(Connection/@ToID) &lt; 8)]">

        <xsl:variable name = "ToID" select = "./Connection/@ToID" />    
        <xsl:variable name = "FromID" select = "./Connection/@FromID" />
        <xsl:variable name = "IBDFrom" select = "concat(substring($FromID,1,1),'-',substring-after($FromID,'-'))" />
        <xsl:variable name = "IBDTo" select = "concat(substring($ToID,1,1),'-',substring-after($ToID,'-'))" />
        <xsl:variable name = "CountTo" select = "count(/PlantModel/SignalLine/Connection[((@FromID=$ToID) or (@ToID=$ToID)) and (concat(substring(@FromID,1,1),'-',substring-after(@FromID,'-'))!=concat(substring(@ToID,1,1),'-',substring-after(@ToID,'-')))]) " />

        <xsl:if test = "($IBDTo != $IBDFrom) and ($CountTo &gt; 1)">

            <Block>                 
                <xsl:attribute  name = "Id" >
                    <xsl:value-of select="$IBDFrom" /><xsl:value-of select="$IBDTo" />
                </xsl:attribute>

                 <ModelChildren>

                    <xsl:for-each select = "/PlantModel/InstrumentLoop/Association[(concat(substring(@TagName,1,1),'-',substring-after(@TagName,'-'))=$IBDFrom) or (concat(substring(@TagName,1,1),'-',substring-after(@TagName,'-'))=$IBDTo)]">

                        <Block>
                            <xsl:attribute  name = "Id" >
                                <xsl:value-of select="@TagName" />
                            </xsl:attribute>
                        </Block>
                    </xsl:for-each>
                </ModelChildren>
            </Block>
        </xsl:if>
    </xsl:for-each>
</Models>
</Project>
</xsl:template>
</xsl:stylesheet>

而且,输出XML是这样的,任何想法?

<Project>
<Models>
    <Block Id="P-109P-110">
        <ModelChildren>
            <Block Id="PIT-109"/>
            <Block Id="PI-109"/>
            <Block Id="PDY-110"/>
            <Block Id="PDI-110"/>
        </ModelChildren>
    </Block>
    <Block Id="P-108P-110">
        <ModelChildren>
            <Block Id="PIT-108"/>
            <Block Id="PI-108"/>
            <Block Id="PDY-110"/>
            <Block Id="PDI-110"/>
        </ModelChildren>
    </Block>
    <Block Id="P-108P-111">
        <ModelChildren>
            <Block Id="PIT-108"/>
            <Block Id="PI-108"/>
            <Block Id="PDY-111"/>
            <Block Id="PDI-111"/>
        </ModelChildren>
    </Block>
    <Block Id="P-107P-111">
        <ModelChildren>
            <Block Id="PIT-107"/>
            <Block Id="PI-107"/>
            <Block Id="PDY-111"/>
            <Block Id="PDI-111"/>
        </ModelChildren>
    </Block>
</Models>
</Project>

感谢您的帮助。

0 个答案:

没有答案