将 html Inside 标记 (span class=) 转换为 XML 标记



我总是找到如何将标签转换为内部标签,但我未能做相反的事情。

我有:

<entry xmlns="http://www.w3.org/2005/Atom">
<title>Entry1</title>
<link rel="self" href="https://google.com"/>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<span class="firstname">John</span>
<span class="lastname">Doe</span>
<div class="update_information">
<span class="update_date">2018-05-05T04:05:03Z</span>
<span class="update_author">xxx@google.com</span>
</div>
</div>
</content>

有了这个XLST:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:for-each select="@*[local-name()!='span']">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:apply-templates select="*|text()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

我检索数据

<entry>
<title>Entry1</title>
<link>
<rel>self</rel>
<href>https://google.com</href>   </link>
<content>
<type>xhtml</type>
<div>
<span><class>firstname</class>John</span>
<span><class>lastname</class>Doe</span>
<div>
<class>update_information</class>
<span><class>update_date</class>2018-05-05T04:05:03Z</span>
<span><class>update_author</class>xxx@google.com</span>
</div>
</div>
</content>

但我期待有:

<entry>
<title>Entry1</title>
<link> <href>https://google.com</href>  </link>
<content>
<type>xhtml</type>
<div>
<firstname>John</firstname>
<lastname>Doe</lastname>
<language>en</language>
<div>
<class>update_information</class>
<update_date>2018-05-05T04:05:03Z</update_date>
<update_author>xxx@google.com</update_author>
</div>
</div>
</content>

正如我们所看到的,我在 span 中提取了值,但现在需要使用它创建一个标签。

实际上,我需要将<span class="firstname">John</span>转换为<firstname>John</firstname>.

谢谢你帮助我。

问候

我建议你试试这种方式:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="x:span">
<xsl:element name="{@class}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

XML:

<entry xmlns="http://www.w3.org/2005/Atom">
<title>Entry1</title>
<updated>2018-05-05T04:05:03Z</updated>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<span class="firstname">John</span>
<span class="lastname">Doe</span>
<div class="orgaloc">
<span class="orgaloc_name">Google</span>
</div>
<div class="update_information">
<span class="update_date">2018-05-05T04:05:03Z</span>
<span class="update_author">xxx@google.com</span>
</div>
</div>
</content>
</entry>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/2005/Atom">
<xsl:output method = "xml" indent="no"/>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="text()[normalize-space()][parent::*]">
<xsl:choose>
<xsl:when test="parent::*/@class">
<xsl:element name="{parent::*/@class}">
<xsl:value-of select="normalize-space(.)"/>
</xsl:element>
</xsl:when>
<xsl:otherwise><xsl:element name="{parent::*/name()}"><xsl:value-of select="normalize-space(.)"/></xsl:element></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*[child::node()]">
<xsl:choose>
<xsl:when test="name()='entry'">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:when>
<xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

最新更新