【问题标题】:XSLT to Get value of a single attribute among multiple attributes to same field valueXSLT获取多个属性中单个属性的值到相同的字段值
【发布时间】:2020-07-13 02:58:04
【问题描述】:

我有一个xml

<PiecesTransportationQuantity unitCode="EA" text="1.0"/>

需要将“text”属性的值转换为相同的字段值,属性unitCode应保留为属性:

<PiecesTransportationQuantity unitCode="EA"> 1.0 </PiecesTransportationQuantity>

请求提供相同的 xslt。

我用过

<xsl:template match="PiecesTransportationQuantity/@unitCode/@text">
    <xsl:element name="{name()}">
        <xsl:value-of select="."/>
    </xsl:element>

模板>

【问题讨论】:

  • 模式PiecesTransportationQuantity/@unitCode/@text 永远不会匹配任何东西:一个属性不能是另一个属性的子属性。
  • 不能使用像/@unitCode/@text 这样的模式。正如上面@michael.hor257k 指出的那样,您可以一次使用一个属性来匹配。

标签: xml xslt attributes


【解决方案1】:

如果您的样式表也有 identity transform 模板,您可以这样做:

<xsl:template match="PiecesTransportationQuantity/@text">
    <xsl:value-of select="."/>
</xsl:template>

否则,您需要执行以下操作:

<xsl:template match="PiecesTransportationQuantity">
    <xsl:copy>
        <xsl:copy-of select="@unitCode"/>
        <xsl:value-of select="@text"/>
    </xsl:copy>
</xsl:template>

【讨论】:

    【解决方案2】:

    你可以这样做:

    <xsl:template match="PiecesTransportationQuantity">
        <xsl:copy>
            <xsl:attribute name="unitCode" select="@unitCode"/>
            <xsl:value-of select="@text"/>
        </xsl:copy>
    </xsl:template>
    

    【讨论】:

    • 您假设 OP 可以使用 XSLT 2.0 或更高版本。
    【解决方案3】:

    模板中使用的模式(同时使用两个属性)匹配不会匹配任何内容。

    如果您想使用您的 Code Pattern,请尝试:

    
    <xsl:template match="PiecesTransportationQuantity">
        <xsl:element name="{name()}">
            <xsl:copy-of select="@* except @text"/>
            <xsl:value-of select="@text"/>
        </xsl:element>
      </xsl:template>
    

    否则,您可以使用@michael.hor257k 回答中的第二条建议:

    
    

    【讨论】:

    • 1.这里不用xsl:element;你可以简单地做xsl:copy,甚至是文字&lt;PiecesTransportationQuantity&gt;。 -- 2. 你也假设 XSLT 2.0。
    • 对@michael.hor257k,刚刚使用了xsl:element的OP模式,这就是为什么提到你的ans。谢谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2018-02-25
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多