加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何执行XSL以从xml标记中删除文本

发布时间:2020-12-16 22:47:34 所属栏目:百科 来源:网络整理
导读:我有一个 XML: ?xml version="1.0" encoding="UTF-8"?COLLECTIONAddedpartweight5kg/weight/partpartweight/weight/partpartweight2kg/weight/part/AddedUpdatepartweight/weight/partpartweight3kg/weight/partpartweight2kg/weight/part/Update/COLLECTIO
我有一个 XML:

<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Added>
<part>
<weight>5kg</weight>

</part>
<part>
<weight></weight>
</part>
<part>
<weight>2kg</weight>
</part>
</Added>
<Update>
<part>
<weight></weight>
</part>
<part>
<weight>3kg</weight>
</part>
<part>
<weight>2kg</weight>
</part>
</Update>
</COLLECTION>

我想在它上面应用XSL,以便< weight>标签需要转换为权重并考虑唯一的数字.这样我想添加一些额外的标签,这在输入XML中不存在.< unitWeights>如果< weight>的值只是预定,然后它会显示KG休息它将是空白的.

预期产量:

<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Added>
<part>
<Weight>5</Weight>
<unitWeights>KG</unitWeights>
</part>
<part>
<Weight></Weight>
<unitWeights></unitWeights>
</part>
<part>
<Weight>2</Weight>
<unitWeights>KG</unitWeights>
</part>
</Added>
<Update>
<part>
<Weight></Weight>
<unitWeights></unitWeights>
</part>
<part>
<Weight>3</Weight>
<unitWeights>KG</unitWeights>
</part>
<part>
<Weight>2</Weight>
<unitWeights>KG</unitWeights>
</part>
</Update>
</COLLECTION>

为此我申请了:

<xsl:template match="Weight">
    <weight>
      <xsl:value-of select="translate(.,translate(.,'0123456789',''),'')"/>
    </weight>
  </xsl:template>
  <xsl:template match="WeightUnits">
    <unitWeights>KG</unitWeights>
    </xsl:template>

请帮我解决我错的地方.

解决方法

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="weight">
    <Weight>
      <xsl:copy-of select="substring-before(.,'kg')"/>
    </Weight>

    <unitWeights>
      <xsl:copy-of select="substring(.,string-length(.) - 1)"/>
    </unitWeights>
  </xsl:template>

  <xsl:template match="unitWeights"/>

</xsl:stylesheet>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读