转换内容位于 CDATA 内的 xml 元素

发布于 2024-08-18 02:15:24 字数 486 浏览 10 评论 0原文

我有一个像下面这样的 xml 片段

<Detail uid="6">
    <![CDATA[
    <div class="heading">welcome to my page</div>
    <div class="paragraph">this is paraph</div>
    ]]>
</Detail>

,我希望能够更改

<div class="heading">...</div> to <h1>Welcome to my page</h1>
<div class="paragraph">...</div> to <p>this is paragraph</p>

你知道我如何在 xslt 1.0 中做到这一点吗

I have a xml fragment like below

<Detail uid="6">
    <![CDATA[
    <div class="heading">welcome to my page</div>
    <div class="paragraph">this is paraph</div>
    ]]>
</Detail>

and I want to be able to change the

<div class="heading">...</div> to <h1>Welcome to my page</h1>
<div class="paragraph">...</div> to <p>this is paragraph</p>

do you know how I can do that in xslt 1.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-08-25 02:15:24

运行两个转换怎么样?

通过 1.)

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

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

    <xsl:template match="Detail">
        <Detail>
            <xsl:copy-of select="@*"/>
        <xsl:value-of select="." disable-output-escaping="yes" />
        </Detail>
    </xsl:template>

</xsl:stylesheet>

将产生:

<?xml version="1.0" encoding="UTF-8"?>
<Detail uid="6"> 
    <div class="heading">welcome to my page</div>
    <div class="paragraph">this is paraph</div>
</Detail>

通过 2.)

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

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

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

    <xsl:template match="div[@class='heading']">
        <h1><xsl:value-of select="."/></h1>
    </xsl:template>

    <xsl:template match="div[@class='paragraph']">
        <p><xsl:value-of select="."/></p>
    </xsl:template>

</xsl:stylesheet>

产生:

<?xml version="1.0" encoding="UTF-8"?>
<Detail uid="6">
<h1>welcome to my page</h1>
<p>this is paraph</p>
</Detail>

What about running two transforms.

Pass 1.)

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

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

    <xsl:template match="Detail">
        <Detail>
            <xsl:copy-of select="@*"/>
        <xsl:value-of select="." disable-output-escaping="yes" />
        </Detail>
    </xsl:template>

</xsl:stylesheet>

Will produce:

<?xml version="1.0" encoding="UTF-8"?>
<Detail uid="6"> 
    <div class="heading">welcome to my page</div>
    <div class="paragraph">this is paraph</div>
</Detail>

Pass 2.)

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

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

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

    <xsl:template match="div[@class='heading']">
        <h1><xsl:value-of select="."/></h1>
    </xsl:template>

    <xsl:template match="div[@class='paragraph']">
        <p><xsl:value-of select="."/></p>
    </xsl:template>

</xsl:stylesheet>

Produces:

<?xml version="1.0" encoding="UTF-8"?>
<Detail uid="6">
<h1>welcome to my page</h1>
<p>this is paraph</p>
</Detail>
抱着落日 2024-08-25 02:15:24

您无法告诉 XSL 1.0 从 CDATA 中获取字符串并将其解析为 XML。

You cannot tell XSL 1.0 to fish a string out of a CDATA and parse it as XML.

硪扪都還晓 2024-08-25 02:15:24

您无法“删除”CDATA,但您可以粗略地实现所需的输出:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
   <Detail>
        <xsl:variable name="before" select="substring-before(//Detail,'<div class="heading">')" />
        <xsl:variable name="afteropen" select="substring-after(//Detail,'<div class="heading">')" />
        <xsl:variable name="body" select="substring-before($afteropen, '</div>')" />
        <xsl:variable name="after" select="substring-after($afteropen, '</div>')" />
        <xsl:value-of select="concat($before, '<h1>', $body, '</h1>',$after)"
                disable-output-escaping="yes"       />
   </Detail>
</xsl:template>
</xsl:stylesheet>

这适用于您尝试解析的第一种类型的 div,并且您可以对第二种类型进行类似的操作。经过一些努力,它可以变得更加通用。

You can't "remove" the CDATA, but you can achieve the desired output somewhat crudely:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
   <Detail>
        <xsl:variable name="before" select="substring-before(//Detail,'<div class="heading">')" />
        <xsl:variable name="afteropen" select="substring-after(//Detail,'<div class="heading">')" />
        <xsl:variable name="body" select="substring-before($afteropen, '</div>')" />
        <xsl:variable name="after" select="substring-after($afteropen, '</div>')" />
        <xsl:value-of select="concat($before, '<h1>', $body, '</h1>',$after)"
                disable-output-escaping="yes"       />
   </Detail>
</xsl:template>
</xsl:stylesheet>

This will work for the first type of div you're trying to parse and you can follow something similar with the second one. It could be made more generic with some effort.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文