首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT-1.0如何编辑标记元素数据

XSLT-1.0如何编辑标记元素数据
EN

Stack Overflow用户
提问于 2018-09-14 13:52:04
回答 1查看 57关注 0票数 0

我正在使用xsl将xml转换为xml。你能帮我写一段将输入转换成输出的xsl代码吗?我需要的数据作为丰富的文本数据在CDATA的前两个标记。提前谢谢。

注意:我有来自Martin @ XSLT-1.0 How to pick multiple tags between two similar tags as it is?的解决方案,但现在我需要编辑值。如何做到这一点?请帮帮忙。

输入:

代码语言:javascript
复制
<ATTRIBUTE-VALUE>
    <THE-VALUE>
        <div xmlns="http://www.w3.org/1999/xhtml">
            <h1 dir="ltr" id="_1536217498885">Main Description</h1>
            <p>Line1 The main description text goes here.</p>
            <p>Line2 The main description text goes here.</p>
            <p>**<img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166?accept=none&amp;private"/>**</p>
            <h1 dir="ltr" id="_1536217498886">Key Consideration</h1>
            <p>Line1 The key consideration text goes here.</p>
            <p>Line2 The key consideration text goes here.</p>
            <h1 dir="ltr" id="_1536217498887">Skills</h1>
            <p>Line1 The Skills text goes here.</p>
            <p>Line2 The Skills text goes here.</p>
            <p>Line3 The Skills text goes here.</p>
            <h1 dir="ltr" id="_1536217498888">Synonyms</h1>
            <p>The Synonyms text goes here.</p>
        </div>
    </THE-VALUE>
</ATTRIBUTE-VALUE>

输出:

代码语言:javascript
复制
<MainDescription>
    <![CDATA[
        <p>Line1 The main description text goes here.</p>
        <p>Line2 The main description text goes here.</p>
        <p>**<img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166.jpg"/>**</p>
    ]]>
</MainDescription>
<KeyConsiderations>
    <![CDATA[
        <p>Line1 The key consideration text goes here.</p>
        <p>Line2 The key consideration text goes here.</p>
    ]]>
</KeyConsiderations>
<Skills>
    <p>Line1 The Skills text goes here.</p>
    <p>Line2 The Skills text goes here.</p>
    <p>Line3 The Skills text goes here.</p>
</Skills>
<Synonyms>
    <p>The Synonyms text goes here.</p>
</Synonyms>
EN

回答 1

Stack Overflow用户

发布于 2018-09-14 16:11:53

如果只想将前两个标记作为CData,只需将此模板添加到您的XSLT中

代码语言:javascript
复制
<xsl:template match="xhtml:h1[position() > 2]">
  <xsl:element name="{translate(., ' ', '')}">
    <xsl:apply-templates select="key('h1-group', generate-id())"/>
  </xsl:element>
</xsl:template>  

或者,如果“前两个标签”只指"MainDescription“和"KeyConsideration",请尝试此模板。

代码语言:javascript
复制
<xsl:template match="xhtml:h1[. != 'Main Description' and . != 'Key Consideration']">
  <xsl:element name="{translate(., ' ', '')}">
    <xsl:apply-templates select="key('h1-group', generate-id())"/>
  </xsl:element>
</xsl:template>  

编辑:如果您还想“编辑”一个子节点,那么只需为您想要修改的元素或属性添加一个模板。对于每个,要修改imgsrc属性,请执行以下操作

代码语言:javascript
复制
<xsl:template match="xhtml:p/xhtml:img/@src">
  <xsl:attribute name="src">
    <xsl:value-of select="concat(substring-before(., '?'), '.jpg')" />
  </xsl:attribute>
</xsl:template>

注意,这只是一个示例,因为您并没有明确说明修改该值需要什么逻辑,所以我假设您只想用文件扩展名".jpg“替换查询字符串部分。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52325687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档