html转xml时如何将元素节点分成两部分并分别应用模板?

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

听起来你并不关心

p
元素的元素内容,除了想要使用
i
元素将
p
元素的内容拆分为两个字符串,组成为:

  • 每个节点的文本值,直到(并包括)
    i
  • i
  • 之后的节点的文本值

...然后使用分隔符; 进一步

标记
第二个字符串。

如果是这样的话,那么这样的事情对你有用:

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

  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="p">
    <xsl:copy>
      <xsl:variable name="until-i" select="(i/preceding-sibling::node(), i)"/>
      <xsl:variable name="after-i" select="i/following-sibling::node()"/>
      <TypeA><xsl:value-of select="normalize-space(string-join($until-i))"/></TypeA>
      <xsl:for-each select="tokenize(string-join($after-i), ';\s')">
        <TypeB><xsl:value-of select="normalize-space(.)"/></TypeB>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<p>
   <TypeA>Quantity:</TypeA>
   <TypeB>250 mg</TypeB>
   <TypeB>150</TypeB>
   <TypeB>170</TypeB>
</p>
© www.soinside.com 2019 - 2024. All rights reserved.