【问题标题】:Can I simplify this XSLT to the less-verbose form?我可以将此 XSLT 简化为不那么冗长的形式吗?
【发布时间】:2016-02-27 12:54:41
【问题描述】:

我的 XSLT 中大多有这样的语句:

<RealisationRelation name='' 
                     xmi.id="{concat($serverxmiid, ':', $infraservicexmiid)}" 
                     xmi.type='I' 
                     from="{$serverxmiid}" 
                     to="{$infraservicexmiid}" />

这不是那么冗长且更易于阅读。但是我该如何创建这样一个简单的语句而不是一个冗长的语句呢?

<xsl:element name="AccessRelation">
    <xsl:attribute name="name">
        <xsl:text></xsl:text>
    </xsl:attribute>
    <xsl:attribute name="xmi.id">
        <xsl:value-of select="concat( $infraservicexmiid, ':', $artifactxmiid)"/>
    </xsl:attribute>
    <xsl:attribute name="xmi.type">
        <xsl:text>I</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="from">
        <xsl:value-of select="$infraservicexmiid"/>
    </xsl:attribute>
    <xsl:attribute name="to">
        <xsl:value-of select="$artifactxmiid"/>
    </xsl:attribute>

    <MM_Profile>
        <xsl:attribute name="name">
            <xsl:text>AccessRelation</xsl:text>
        </xsl:attribute>
    </MM_Profile>
    <MM_Value>
        <xsl:attribute name="name">
            <xsl:text>accessType</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="value">
            <xsl:text>w</xsl:text>
        </xsl:attribute>
    </MM_Value>
</xsl:element>

我想知道如何以不那么冗长的形式获得这些 MM_ProfileMM_Value 部分。其他都很简单。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    使用文字结果元素,就像你已经做的那样,简单地定义属性,例如&lt;MM_Profile name="AccessRelation"/&gt;&lt;MM_Value name="accessType" value="w"/&gt;。如果您需要计算(部分)属性值,请使用属性值模板执行此操作,例如&lt;AccessRelation xmi.id="{$infraservicexmiid}:{$artifactxmiid}" from="{$infraservicexmiid}"&gt;&lt;/AccessRelation&gt;.

    【讨论】:

      【解决方案2】:

      这是您的 XSLT 简化为使用 literal result elementsattribute value templates

      <AccessRelation name=""
                      xmi.id="{$infraservicexmiid}:{$artifactxmiid}"
                      xmi.type="I"
                      from="{$infraservicexmiid}"
                      to="{$artifactxmiid}">
        <MM_Profile name="AccessRelation"/>
        <MM_Value name="accessType" value="w"/>
      </AccessRelation>
      

      这与您已经为 RealisationRelation 所做的相同(Martin Honnen 有帮助地重申和命名,+1),加上文字结果元素可以嵌入其他文字结果元素的概念。

      Creating element nodes using xsl:element 很少见,但在必须计算元素名称而不是提前知道时会很有帮助。

      【讨论】:

      • 您也可以将xmi.id="{concat( $infraservicexmiid, ':', $artifactxmiid)}"替换为xmi.id="{$infraservicexmiid}:{$artifactxmiid}"
      • 是的,这样更好。答案已更新。谢谢。
      • 谢谢!我也可以用&lt;xsl:variable name="clustername" select="concat('[', $cluster,'] ', $clustertype, ' (Node)')"/&gt; 做最后一招吗?在某些时候,我试图这样做,但解析器卡住了,所以我做错了。
      • 不,属性值模板不适用于 select 属性,而是用于文字结果元素,例如此答案中所示。您对xsl:variable/@select 的替代方法是使用xsl:textxsl:value-of 的组合作为xsl:variable 的子代,但这会更冗长,而不是更少。
      • 在 XSLT 3.0(启用扩展文本)中,您可以执行 &lt;xsl:variable name="cn" as="xs:string"&gt;[{$cluster}] {$clustertype} (Node)&lt;/xsl:variable&gt;
      猜你喜欢
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      相关资源
      最近更新 更多