首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xslt将节点/元素作为属性添加到XML节点

如何使用xslt将节点/元素作为属性添加到XML节点
EN

Stack Overflow用户
提问于 2022-09-28 14:44:40
回答 1查看 35关注 0票数 1

我想将XHTML转换成XML,如下所示,但我不知道如何做到这一点。我希望读取输入的div.cmp-text's数据,并将其添加到XML元素中的属性中。

输入XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<result>
    <div class="cmp-text">
        <strong xmlns="http://www.w3.org/1999/xhtml">Content</strong>
        <span xmlns="http://www.w3.org/1999/xhtml"
            class="data-class">May 19, 2020
        </span>
        <h2 xmlns="http://www.w3.org/1999/xhtml">Description</h2>
        <p xmlns="http://www.w3.org/1999/xhtml">
            Lorem ipsum dolor sit amet, consectetur adipisicing.
        </p>
    </div>
    
    <div class="cmp-horizontal-line">
        <hr xmlns="http://www.w3.org/1999/xhtml"/>
    </div>
    
    <div class="cmp-text">
        <ul xmlns="http://www.w3.org/1999/xhtml">
            <li>
                Lorem ipsum.
            </li>
        </ul>
        <table xmlns="http://www.w3.org/1999/xhtml"
            style="border-collapse: collapse;"
            border="1">
            <tbody>
                <tr>
                    <td style="width: 33.3333%;">111</td>
                    <td style="width: 33.3333%;">212</td>
                </tr>
            </tbody>
        </table>
    </div>
    
    <div class="cmp-horizontal-line">
        <hr xmlns="http://www.w3.org/1999/xhtml"/>
    </div>
</result>

预期产出:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<result xmlns:jcr="http://www.jcp.org/jcr/1.0"
    xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    xmlns:mix="http://www.jcp.org/jcr/mix/1.0"
    xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
    xmlns:cq="http://www.day.com/jcr/cq/1.0"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <result>
        <text
            type="/text"
            text="&lt;strong xmlns='http://www.w3.org/1999/xhtml'&gt;Content&lt;/strong&gt;&lt;span xmlns='http://www.w3.org/1999/xhtml' class='data-class'&gt;May 19, 2020&lt;/span&gt;&lt;h2 xmlns='http://www.w3.org/1999/xhtml'&gt;Description&lt;/h2&gt;&lt;p xmlns='http://www.w3.org/1999/xhtml'&gt;Lorem ipsum dolor sit amet, consectetur adipisicing.&lt;/p&gt;"
            textIsRich="true"/>
        <horizontal_line type="/horizontal-line"/>
        <text type="/text"
            text="&lt;ul xmlns='http://www.w3.org/1999/xhtml'&gt;&lt;li&gt;Lorem ipsum.&lt;/li&gt;&lt;/ul&gt;&lt;table xmlns='http://www.w3.org/1999/xhtml' style='border-collapse: collapse;' border='1'&gt;&lt;tbody>&lt;tr>&lt;td style='width: 33.3333%;'>111&lt;/td>&lt;td style='width: 33.3333%;'>212&lt;/td>&lt;/tr>&lt;/tbody>&lt;/table>"
            textIsRich="true"/>
        <horizontal_line type="/horizontal-line"/>
    </result>
</result>

XSL:

代码语言:javascript
复制
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:jcr="http://www.jcp.org/jcr/1.0"
    xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    xmlns:cq="http://www.day.com/jcr/cq/1.0"
    xmlns:mix="http://www.jcp.org/jcr/mix/1.0"
    xmlns:sling="http://sling.apache.org/jcr/sling/1.0">

    <xsl:output version="1.0"
        encoding="UTF-8"
        indent="yes"
        method="xml"
        omit-xml-declaration="no"/>
    <xsl:strip-space elements="*"/>

    <!--root element-->
    <xsl:template match="/">
        <result>
            <xsl:apply-templates/>
        </result>
    </xsl:template>

    <!--template I need help with: it should take the input cmp-text div's content(HTML tags) and add it to the text attribute of text element-->
    <xsl:template match="/result/div[@class='cmp-text']">
        <text>
            <xsl:attribute name="type">/text</xsl:attribute>
            <xsl:attribute name="text">value</xsl:attribute>
            <xsl:attribute name="text2">
                <xsl:value-of select="node()"/>
            </xsl:attribute>
            <xsl:attribute name="text3">
                <xsl:value-of select=".//*"/>
            </xsl:attribute>
        </text>
    </xsl:template>

    <!--horizontal line-->
    <xsl:template match="/result/div[@class='cmp-horizontal-line']">
        <horizontal_line type="/horizontal-line"/>
    </xsl:template>

    <!--horizontal line-->
    <xsl:template match="/result/xhtml:div[@class='cmp-horizontal-line']">
        <horizontal_line type="/horizontal-line"/>
    </xsl:template>

    <!--identity template copies everything forward by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

使用上面的XSL输出XML:

代码语言:javascript
复制
<result xmlns:jcr="http://www.jcp.org/jcr/1.0"
    xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    xmlns:mix="http://www.jcp.org/jcr/mix/1.0"
    xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
    xmlns:cq="http://www.day.com/jcr/cq/1.0"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <result>
        <text type="/text"
            text="value"
            text2="Last Reviewed:"
            text3="Last Reviewed:"/>        
        <horizontal_line type="/horizontal-line"/>
        <text type="/text"
            text="value"
            text2="Criteria"
            text3="Criteria"/>
        <horizontal_line type="/horizontal-line"/>
    </result>
</result>

在文本元素中,属性文本、text2和text3是我获得节点(HTML)的失败尝试,就像在属性中一样。

如何获得所需的输出?

更新:将所需的输出更新为格式良好的。

解决方案需要在XSLT1.0中,因此不能使用序列化()。

在Martin的评论之后,我使用了LenzConsuling.com/xml-to,并能够通过对XSL脚本进行以下更改获得所需的结果:

代码语言:javascript
复制
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">

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

    <xsl:template match="/result/div[@class='cmp-text']">
        <text>
            <xsl:attribute name="type">/text</xsl:attribute>
            <xsl:attribute name="text">
                <xsl:apply-templates select="./*" mode="xml-to-string"/>
            </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

它产生了以下XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<result xmlns:jcr="http://www.jcp.org/jcr/1.0"
    xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    xmlns:mix="http://www.jcp.org/jcr/mix/1.0"
    xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
    xmlns:cq="http://www.day.com/jcr/cq/1.0"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <result>
        <text
            type="/text"
            text="&lt;strong xmlns='http://www.w3.org/1999/xhtml'&gt;Content&lt;/strong&gt;&lt;span xmlns='http://www.w3.org/1999/xhtml' class='data-class'&gt;May 19, 2020&lt;/span&gt;&lt;h2 xmlns='http://www.w3.org/1999/xhtml'&gt;Description&lt;/h2&gt;&lt;p xmlns='http://www.w3.org/1999/xhtml'&gt;Lorem ipsum dolor sit amet, consectetur adipisicing.&lt;/p&gt;"
            textIsRich="true"/>
        <horizontal_line type="/horizontal-line"/>
        <text type="/text"
            text="&lt;ul xmlns='http://www.w3.org/1999/xhtml'&gt;&lt;li&gt;Lorem ipsum.&lt;/li&gt;&lt;/ul&gt;&lt;table xmlns='http://www.w3.org/1999/xhtml' style='border-collapse: collapse;' border='1'&gt;&lt;tbody>&lt;tr>&lt;td style='width: 33.3333%;'>111&lt;/td>&lt;td style='width: 33.3333%;'>212&lt;/td>&lt;/tr>&lt;/tbody>&lt;/table>"
            textIsRich="true"/>
        <horizontal_line type="/horizontal-line"/>
    </result>
</result>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-28 15:51:22

所以XSLT3.0的模板应该是。

代码语言:javascript
复制
<!--template I need help with: it should take the input cmp-text div's content(HTML tags) and add it to the text attribute of text element-->
<xsl:template match="/result/div[@class='cmp-text']">
    <text>
        <xsl:attribute name="type">/text</xsl:attribute>
        <xsl:attribute name="text" select="serialize(*)"/>
    </text>
</xsl:template>

它可以简化为例如。

代码语言:javascript
复制
<!--template I need help with: it should take the input cmp-text div's content(HTML tags) and add it to the text attribute of text element-->
<xsl:template match="/result/div[@class='cmp-text']">
    <text type="/text" text="{serialize(*)}"/>
</xsl:template>

这样,输出就会更像。

代码语言:javascript
复制
  <text type="/text"
        text="&lt;strong xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt;Content&lt;/strong&gt;&lt;span xmlns=&#34;http://www.w3.org/1999/xhtml&#34; class=&#34;data-class&#34;&gt;May 19, 2020&#xA;        &lt;/span&gt;&lt;h2 xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt;Description&lt;/h2&gt;&lt;p xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt;&#xA;            Lorem ipsum dolor sit amet, consectetur adipisicing.&#xA;        &lt;/p&gt;"/>

如果你真的需要走这条路,生成格式不佳的结果,那么在XSLT 3中,字符映射可以帮助你。

代码语言:javascript
复制
   <xsl:output version="1.0"
        encoding="UTF-8"
        indent="yes"
        method="xml"
        omit-xml-declaration="no" use-character-maps="m1"/>
    
    <xsl:character-map name="m1">
      <xsl:output-character character="&lt;" string="&lt;"/>
      <xsl:output-character character="&gt;" string=">"/>
      <xsl:output-character character="&quot;" string="&quot;"/>
    </xsl:character-map>

然后,撒克逊产生的输出类似于。

代码语言:javascript
复制
  <text type="/text"
        text='<strong xmlns="http://www.w3.org/1999/xhtml">Content</strong><span xmlns="http://www.w3.org/1999/xhtml" class="data-class">May 19, 2020&#xA;        </span><h2 xmlns="http://www.w3.org/1999/xhtml">Description</h2><p xmlns="http://www.w3.org/1999/xhtml">&#xA;            Lorem ipsum dolor sit amet, consectetur adipisicing.&#xA;        </p>'/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73883490

复制
相关文章

相似问题

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