0

我有一个变量(ImgData)作为值<p><image id="image1" name="firstimage" /></p>

如何将 ImgData 的值更改为<p><image id="m-image1" name="firstimage" />使用 XSLT。

我只想为 id 属性添加前缀或后缀。提前致谢。

编辑:

我的 ImgData 取值为

<xsl:variable name="ImgData">
  <p><?image id="image1" /></p>
</xsl:variable>

如何将 ImgData 的值更改为

<xsl:variable name="ImgData">
      <p><?image id="m-image1" /></p>
    </xsl:variable>

根据 hr_117 评论,我将此添加到我的 xslt 但未显示 id。

<xsl:variable name="sam">
  <xsl:value-of select="translate($ImgData,'?','')" />      
</xsl:variable>
<xsl:value-of select="$sam"/>
<xsl:value-of select="exsl:node-set($sam)//image/@id" />

我可以在没有“?”的情况下打印 Imgdata 值。不知道为什么 x-path 不起作用。请建议。

4

3 回答 3

1

ImagData 似乎是一个字符串。因此,使用 xlst-1.0 做某事的唯一可能性就像这个丑陋的选择:

<xsl:value-of select=" concat(
                          substring-before($ImgData, substring-after($ImgData,'id=&quot;')),
                          'm-',
                          substring-after($ImgData,'id=&quot;')
                      ) "
                       disable-output-escaping="yes"
                      />

这仅在字符串变量中只有一个 id 时才有效。这也可能产生:

 <p><?image id="m-image1" /></p>

但我不会推荐这种方式。

于 2013-05-07T15:21:32.907 回答
1

使用 concat("Navin", "Rawat") 之类的 concat 函数来获取输出 "Navin Rawat"

于 2013-05-07T09:50:54.690 回答
1

至少存在三个问题。
* 你的变量内容不是格式良好的xml,节点名称不能以<?这是一个处理指令的开始。
* 您不能使用 xlst-1.0 的 xslt 文件中的 xml 访问变量的内容。这只能通过扩展名来实现,例如“exsl:node-set”。

试试这个来访问图像的 id 属性。

 <?xml version="1.0"?>
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:exsl="http://exslt.org/common"
   extension-element-prefixes="exsl"
   version="1.0">

    <xsl:variable name="ImgData">
        <p>
            <image id="image1" />
        </p>
    </xsl:variable>

    <xsl:template match="/" >
        <xsl:value-of select="exsl:node-set($ImgData)//image/@id"/>
    </xsl:template>

</xsl:stylesheet>
  • 您不能更改 xslt 变量的值。您唯一能做的就是在旧的基础上创建一个新的,并更改内容。

更新:创建具有更改内容的新变量的示例。

<xsl:template match="/" >
        <xsl:variable name="NewImgData">
            <xsl:apply-templates select="exsl:node-set($ImgData)" mode="new-var" />
        </xsl:variable>
    </xsl:template>

    <xsl:template match="image/@id" mode="new-var">
        <xsl:attribute name="id" >
            <xsl:value-of select="concat('m-',.)"/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="@*| node()" mode="new-var">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="new-var"/>
        </xsl:copy>
    </xsl:template>

NewImgData 的内容现在是:

<p><image id="m-image1"/></p>
于 2013-05-07T12:03:42.820 回答