首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT-1.0如何在两个相似的标记之间选择多个标记?

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

Stack Overflow用户
提问于 2018-09-07 18:31:07
回答 1查看 223关注 0票数 0

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

输入:

代码语言:javascript
复制
<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>

输出:

代码语言:javascript
复制
<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中获取元素。但我不知道如何获取“

”的值,所以我将其标记为?请帮助获得解决方案?

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-07 22:53:03

可以使用键将h1元素后面的同级元素包装到从XSLT1中的h1元素创建的包装器元素中:

代码语言:javascript
复制
<?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

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

代码语言:javascript
复制
<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这样的扩展函数

代码语言:javascript
复制
<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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52220631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档