需要使用XSLT从属性值中删除一些文本

时间:2017-02-09 06:39:58

标签: xml xslt xslt-2.0

我需要使用XSLT

从属性值中删除一部分文本

我使用的XML:

<img imageid="47" alt="cup." height="300" width="400" class="right" src="https://tneb.com/Services/Gets/Contents/ucr-images-v1/Images/cup-36" />

我使用的XSL:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all">

      <xsl:template match="img">
        <xsl:element name="image">
          <xsl:attribute name="id">
            <xsl:value-of select="@imageid"/>
          </xsl:attribute>
          <xsl:attribute name="alt">
            <xsl:value-of select="@alt"/>
          </xsl:attribute>
          <xsl:attribute name="height">
            <xsl:value-of select="@height"/>
          </xsl:attribute>
          <xsl:attribute name="width">
            <xsl:value-of select="@width"/>
          </xsl:attribute>
          <xsl:attribute name="align">
            <xsl:value-of select="@class"/>
          </xsl:attribute>
          <xsl:attribute name="href">
            <xsl:value-of select="@src"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:template>

 </xsl:stylesheet>

输出我得到:

 <image id="47"
 alt="cup."
 height="300"
 width="400"
 align="right"
 href="https://tneb.com/Services/Gets/Contents/ucr-images-v1/Images/cup-36"/>

预期输出必须是:

<image id="47"
 alt="cup."
 height="300"
 width="400"
 align="right"
 href="/ucr-images-v1/Images/cup-36"/>

我需要从图像的属性值中删除一些文本。最后3只表示文件夹结构。所以我只需要自己。

请给我任何建议。提前谢谢。

1 个答案:

答案 0 :(得分:1)

要仅获取路径的最后3个位置步骤,请更改:

<xsl:value-of select="@src"/>

为:

<xsl:value-of select="tokenize(@src, '/')[position() gt last() - 3]" separator="/"/>