【问题标题】:XSLT 1.0 check if element exists and append new childXSLT 1.0 检查元素是否存在并追加新的子元素
【发布时间】:2014-05-20 09:04:09
【问题描述】:

我有以下源 xml 文件,并想用 xsl 将其转换为另一个 xml 文件。 如何检查元素是否存在并根据此结果创建新元素或将子元素附加到现有元素?

<Vortriebsorte>
    <Vortriebsort>
        <Name>Südröhre</Name>
        <Bauphase>Kalotte</Bauphase>  
        <Vortrieb>Südröhre</Vortrieb>     
    </Vortriebsort>
    <Vortriebsort>
        <Name>Nordröhre</Name>
        <Bauphase>Strosse</Bauphase>     
        <Vortrieb>Nordröhre</Vortrieb>
    </Vortriebsort>
    <Vortriebsort>
        <Name>Südröhre</Name>
        <Bauphase>Strosse / Sohle</Bauphase>
        <Vortrieb>Südröhre</Vortrieb>
    </Vortriebsort>
</Vortriebsorte>

result-xml-file 应该类似于以下 sn-p:

<data>
<group name="Abschlagsdaten">
    <group name="Vortrieb: Südröhre">
        <group name="Bauphase: Kalotte">
          <!--- some more stuff -->
        </group>          
        <group name="Bauphase: Strosse / Sohle">
          <!--- some more stuff -->
        </group>
    </group>
    <group name="Vortrieb: Nordröhre">                
        <group name="Bauphase: Strosse / Sohle">
          <!--- some more stuff -->
        </group>
    </group>
</group>
</data>

元素“Bauphase”应按结果文件中的元素“Vortrieb”分组。 我能够在不按“Vortrieb”分组的情况下产生有效的输出。

由于我是 xsl 转换的新手,如果可能的话,如果有人可以给我一个提示,我将不胜感激,如果可以的话如何?

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />

<xsl:template match="@*|node()">            
    <xsl:apply-templates select="@*|node()"/>           
</xsl:template>

<xsl:template match="Vortriebsorte">    
    <data>
        <xsl:for-each select="Vortriebsort">
        <!-- if contains element append new element <group name="Bauphase: ..."> to the existing element -->   
        <xsl:if test="(contains(@name, \"Vortrieb: \"<xsl:value-of select=\"Vortrieb\" />))">
            <group>
                <xsl:attribute name="name">Abschlag: <xsl:value-of select="Bauphase" /> </xsl:attribute>
                <xsl:apply-templates select="Tunnelbandgruppen/Tunnelbandgruppe"/>  
            </group>
            </xsl:if>
        <!-- if NOT contains element add <group name="Vortrieb: ..."> and apply templates for child-nodes -->   
        <xsl:if test="not(contains(@name, \"Vortrieb: \"<xsl:value-of select=\"Vortrieb\" />))">
            <group>
                <xsl:attribute name="name">Vortrieb: <xsl:value-of select="Vortrieb" /> </xsl:attribute>    
                <group>
                    <xsl:attribute name="name">Abschlag: <xsl:value-of select="Bauphase" /> </xsl:attribute>
                    <xsl:apply-templates select="Tunnelbandgruppen/Tunnelbandgruppe"/>  
                </group>
            </group>                
            </xsl:if>-->
        </xsl:for-each>
        </data>
    </tunneltracer-exchange-file>
</xsl:template>

</xsl:stylesheet>

亲切的问候, 马库斯

【问题讨论】:

    标签: xml xslt-1.0


    【解决方案1】:

    您可以尝试使用此模板:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:strip-space elements="*" />
        <xsl:output method="xml" indent="yes" />
        <xsl:template match="/">
            <data>
                <group name="Abschlagsdaten">
                    <xsl:apply-templates select="Vortriebsorte/Vortriebsort[not(./Name=preceding::*/Name)]"/>
                </group>
            </data>
        </xsl:template>
        <xsl:template match="Vortriebsort">
            <xsl:variable name="n" select="./Name"/>
            <group>
                <xsl:attribute name="name">
                    <xsl:value-of select="'Vortrieb: '"/>
                    <xsl:value-of select="./Name"/>
                </xsl:attribute>
                <xsl:apply-templates select="//Bauphase[../Name = $n]"/>
            </group>
        </xsl:template>
        <xsl:template match="Bauphase">
            <group>
                <xsl:attribute name="name">
                    <xsl:value-of select="'Bauphase: '"/>
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </group>
        </xsl:template>
    </xsl:stylesheet>
    

    它只会通过Name找到不同的Vortriebsort节点,然后搜索与Name具有相同值的所有Bauphase节点。

    它将生成这个 XML:

    <data>
        <group name="Abschlagsdaten">
            <group name="Vortrieb: Südröhre">
                <group name="Bauphase: Kalotte" />
                <group name="Bauphase: Strosse / Sohle" />
            </group>
            <group name="Vortrieb: Nordröhre">
                <group name="Bauphase: Strosse" />
            </group>
        </group>
    </data>
    

    【讨论】:

    • 使用preceding:: 进行分组是一个 O(N^2) 操作,与 Muenchian 分组相比,通常不建议使用。
    【解决方案2】:

    您可以在 XSLT 1.0 中使用Muenchian grouping 进行分组。

    这是一个使用该技术产生预期结果的样式表:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
        <xsl:output indent="yes"/>
    
        <xsl:key name="vortrieb-group" match="Vortrieb" use="."/>
        <xsl:key name="bauphase-group" match="Bauphase" use="."/>
    
        <xsl:template match="Vortriebsorte">
            <data>
                <group name="Abschlagsdaten">
                    <xsl:apply-templates/>
                </group>
            </data>
        </xsl:template>
    
        <xsl:template match="Vortriebsort">
            <xsl:apply-templates select="Vortrieb[generate-id(.)=generate-id(key('vortrieb-group', .))]"/>
        </xsl:template>
    
        <xsl:template match="Vortrieb">
            <group name="Vortrieb: {.}">
                <xsl:apply-templates select="//Bauphase[../Vortrieb=current()][generate-id(.)=generate-id(key('bauphase-group', .))]"/>
            </group>
        </xsl:template>
    
        <xsl:template match="Bauphase">
            <group name="Bauphase: {.}">
                <xsl:value-of select="."/>
            </group>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 2011-04-14
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      相关资源
      最近更新 更多