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

问题描述 投票:0回答:1

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

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

输入:

<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>

输出:

<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>
xml xslt xslt-1.0
1个回答
0
投票

如果您只想将前两个标签作为CData,只需将此模板添加到XSLT即可

<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”,请尝试使用此模板....

<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>  

编辑:如果您也想“编辑”其中一个子节点,则只需为要修改的元素或属性添加模板。对于每一个,要修改srcimg属性,请执行此操作

<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”替换查询字符串部分。

© www.soinside.com 2019 - 2024. All rights reserved.