XSLT-1.0 如何在两个相似标签之间选择多个标签?

维沙尔·沙纳加特

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

输入:

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

我可以使用以下代码从 h1 获取元素。但是我不知道获取 '< p >' 的值,所以我将它标记为 ?????????。请帮助解决?????????。

<xsl:for-each select="my:THE-VALUE/xhtml:div/xhtml:h1">
    <xsl:variable name="ReqIFTextTags" select="translate(., ' ', '')"></xsl:variable>
    <xsl:element name="{$ReqIFTextTags}">
        <xsl:value-of select="?????????"></xsl:value-of>
    </xsl:element>
</xsl:for-each>
马丁·霍南

可以使用键h1元素后面的兄弟元素包装到从h1XSLT 1 中的元素创建的包装器元素中

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xhtml"
    version="1.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:key name="h1-group" match="xhtml:div/*[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>

  <xsl:template match="xhtml:div[xhtml:h1]">
      <xsl:apply-templates select="xhtml:h1"/>
  </xsl:template>

  <xsl:template match="xhtml:h1">
      <xsl:element name="{translate(., ' ', '')}">
          <xsl:apply-templates select="key('h1-group', generate-id())"/>
      </xsl:element>
  </xsl:template>

  <xsl:template match="xhtml:p">
      <p>
          <xsl:apply-templates/>
      </p>
  </xsl:template>

</xsl:stylesheet>

在线https://xsltfiddle.liberty-development.net/bdxtqy

将这些元素的内容序列化为标记最好使用扩展函数(如果您使用的 XSLT 1 处理器有一个或很容易允许设置它)或用于该任务的库,如http://lenzconsulting.com/xml-to -string/xml-to-string.xsl,然后您可以序列化元素:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xhtml"
    version="1.0">

  <xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>

  <xsl:output method="xml" indent="yes"
    cdata-section-elements="MainDescription KeyConsideration"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
      <xsl:apply-templates/>
  </xsl:template>

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

  <xsl:key name="h1-group" match="xhtml:div/*[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>

  <xsl:template match="xhtml:div[xhtml:h1]">
      <xsl:apply-templates select="xhtml:h1"/>
  </xsl:template>

  <xsl:template match="xhtml:h1">
      <xsl:element name="{translate(., ' ', '')}">
          <xsl:apply-templates select="key('h1-group', generate-id())" mode="xml-to-string"/>
      </xsl:element>
  </xsl:template>


</xsl:stylesheet>

拥有 CDATA 部分需要提前知道元素并提前命名,例如<xsl:output cdata-section-elements="MainDescription KeyConsideration"/>,正如我在上面的示例中所做的,也在https://xsltfiddle.liberty-development.net/bdxtqy/1在线

由于您在 XHTML 命名空间中有原始元素,但您想要的输出p在没有命名空间中具有序列化元素,因此您首先需要通过剥离命名空间的模板推送元素,然后将它们推送到 mode xml-to-string,这还需要使用的扩展功能,如exsl:node-set

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:exsl="http://exslt.org/common"
    exclude-result-prefixes="xhtml exsl"
    version="1.0">

  <xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>

  <xsl:output method="xml" indent="yes"
    cdata-section-elements="MainDescription KeyConsideration"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
      <xsl:apply-templates/>
  </xsl:template>

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

  <xsl:key name="h1-group" match="xhtml:div/*[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>

  <xsl:template match="xhtml:div[xhtml:h1]">
      <xsl:apply-templates select="xhtml:h1"/>
  </xsl:template>

  <xsl:template match="xhtml:h1">
      <xsl:element name="{translate(., ' ', '')}">
          <xsl:variable name="rtf-with-xhtml-ns-stripped">
              <xsl:apply-templates select="key('h1-group', generate-id())"/>
          </xsl:variable>
          <xsl:apply-templates select="exsl:node-set($rtf-with-xhtml-ns-stripped)/node()" mode="xml-to-string"/>
      </xsl:element>
  </xsl:template>

  <xsl:template match="xhtml:p">
      <p>
          <xsl:apply-templates/>
      </p>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bdxtqy/2

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在python中以两种不同的颜色显示来自两个标签(0,1)的数值?

来自分类Dev

通过XSLT在两个自闭合标签之间选择文本

来自分类Dev

如何使用XSLT-1从格式文本中分离标签?

来自分类Dev

模板标签需要0个参数,提供了1个

来自分类Dev

用于生成 [((0,0),0), ((0,1),0), ((1,0),0), ((1,1),0)] 的代码实际上给出了 [0 , 0, 0, 1, 1, 0, 1, 1],如何解决?

来自分类Dev

xslt 1.0如何在选择条件下用0(零)替换空或空白值

来自分类Dev

为什么为 dx[dir 选择值 {1, 1, 0, -1, -1, -1, 0, 1} 和 {0, 1, 1, 1, 0, -1, -1, -1} ] 和 dy[dir]?

来自分类Dev

计算C#中两个1之间的0的最高数目

来自分类Dev

标签长度2应该为1或0

来自分类Dev

如何在xslt 1.0中仅获取两个日期之间的工作日

来自分类Dev

如何在xslt中找到两个xml之间的区别

来自分类Dev

如何在XSLT中获取标签值

来自分类Dev

XSLT转换以对相似标签进行分组

来自分类Dev

XSLT复制-从1个节点创建2个节点

来自分类Dev

XSLT复制-从1个节点创建2个节点

来自分类Dev

使用xslt 1显示X个节点数

来自分类Dev

随机选择0或1个相等的次数?

来自分类Dev

如何使用XSLT在日期中增加1天

来自分类Dev

如何使用XSLT转换/翻译/替换'[AZ]','[1-26]'

来自分类Dev

如何使用XSLT在日期中增加1天

来自分类Dev

如何生成仅包含给定长度的两个1和其他0的列表?

来自分类Dev

如何比较两个csv文件并使用python在新文件中写入1或0

来自分类Dev

如何在两个主键之间做出选择 1 如果其他键有值,则为 null?

来自分类Dev

使用 xslt 1.0 从两个不同的父标签中查找不同的元素

来自分类Dev

基于多个标签及其值的XSLT值选择

来自分类Dev

如何绘制y在0,1之间?

来自分类Dev

如何获取0到1之间的小数?

来自分类Dev

XSLT浏览标签级别

来自分类Dev

有两个单选按钮(是或否),其值分别为0或1以输入数据库。如何用“是”或“否”呼应用户的选择?

Related 相关文章

  1. 1

    如何在python中以两种不同的颜色显示来自两个标签(0,1)的数值?

  2. 2

    通过XSLT在两个自闭合标签之间选择文本

  3. 3

    如何使用XSLT-1从格式文本中分离标签?

  4. 4

    模板标签需要0个参数,提供了1个

  5. 5

    用于生成 [((0,0),0), ((0,1),0), ((1,0),0), ((1,1),0)] 的代码实际上给出了 [0 , 0, 0, 1, 1, 0, 1, 1],如何解决?

  6. 6

    xslt 1.0如何在选择条件下用0(零)替换空或空白值

  7. 7

    为什么为 dx[dir 选择值 {1, 1, 0, -1, -1, -1, 0, 1} 和 {0, 1, 1, 1, 0, -1, -1, -1} ] 和 dy[dir]?

  8. 8

    计算C#中两个1之间的0的最高数目

  9. 9

    标签长度2应该为1或0

  10. 10

    如何在xslt 1.0中仅获取两个日期之间的工作日

  11. 11

    如何在xslt中找到两个xml之间的区别

  12. 12

    如何在XSLT中获取标签值

  13. 13

    XSLT转换以对相似标签进行分组

  14. 14

    XSLT复制-从1个节点创建2个节点

  15. 15

    XSLT复制-从1个节点创建2个节点

  16. 16

    使用xslt 1显示X个节点数

  17. 17

    随机选择0或1个相等的次数?

  18. 18

    如何使用XSLT在日期中增加1天

  19. 19

    如何使用XSLT转换/翻译/替换'[AZ]','[1-26]'

  20. 20

    如何使用XSLT在日期中增加1天

  21. 21

    如何生成仅包含给定长度的两个1和其他0的列表?

  22. 22

    如何比较两个csv文件并使用python在新文件中写入1或0

  23. 23

    如何在两个主键之间做出选择 1 如果其他键有值,则为 null?

  24. 24

    使用 xslt 1.0 从两个不同的父标签中查找不同的元素

  25. 25

    基于多个标签及其值的XSLT值选择

  26. 26

    如何绘制y在0,1之间?

  27. 27

    如何获取0到1之间的小数?

  28. 28

    XSLT浏览标签级别

  29. 29

    有两个单选按钮(是或否),其值分别为0或1以输入数据库。如何用“是”或“否”呼应用户的选择?

热门标签

归档