XSLT-1.0如何在两个相似的标记之间选择多个标记? [英] XSLT-1.0 How to pick multiple tags between two similar tags as it is?

查看:4
本文介绍了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元素后面的同级元素包装到根据XSLT1中的h1元素创建的包装元素中:

<?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元素,所以您首先需要通过一个模板推送这些元素,该模板剥离命名空间,然后通过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

这篇关于XSLT-1.0如何在两个相似的标记之间选择多个标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆