我也有一个包含html标记的Json文件

{
"process": "Test",
"title": "Json2XML_Conversion",
"content": "<div id=\"contents\"><p class=\"Head\">This need to process as title</p><p class=\"para\">This is child text</p></div>"
}

我在XSLT中处理过这个问题,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    
    <xsl:output indent="yes"></xsl:output>
    
    
    <xsl:variable name="text" select="unparsed-text('JsonFile.json', 'UTF-8')"/>
    
    <xsl:template match="/">
        <xsl:message select="$text"></xsl:message>
        <xsl:variable name="XML">
            <xsl:apply-templates select="json-to-xml($text)" mode="copy"/>
        </xsl:variable>
        <Root><xsl:apply-templates select="$XML" mode="ContentTest"/></Root>
    </xsl:template>
    
    <xsl:template match="*[@key]" mode="copy">
        <xsl:element name="{@key}">
            <xsl:choose>
                <xsl:when test="@key = 'content'">
                    <xsl:value-of select="." disable-output-escaping="yes"/>
                </xsl:when>
                <xsl:otherwise><xsl:apply-templates mode="copy"/></xsl:otherwise>
            </xsl:choose>
        </xsl:element>
    </xsl:template>
    
    <xsl:mode on-no-match="shallow-copy" name="ContentTest"/>
    
    <xsl:template match="div" mode="ContentTest">
        <RootElementforContent>
            <xsl:apply-templates mode="#current"/>
        </RootElementforContent>
    </xsl:template>
    
</xsl:stylesheet>

当前输出:

    <Root>
   <process>Test</process>
   <title>Json2XML_Conversion</title>
   <content>&lt;div id="contents"&gt;&lt;p class="Head"&gt;This need to process as title&lt;/p&gt;&lt;p class="para"&gt;This is child text&lt;/p&gt;&lt;/div&gt;</content>
</Root>

所需输出:

<Root>
   <process>Test</process>
   <title>Json2XML_Conversion</title>
   <content><RootElementforContent><p class="Head">This need to process as title</p><p class="para">This is child text</p></RootElementforContent></content>
</Root>

这是一个示例输入和输出,我有完整的html struct 的原始文本,所以需要判断,这是可能的处理在单个XSLT.

推荐答案

这给我提供了https://martin-honnen.github.io/xslt3fiddle/的yields

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  expand-text="yes"
  version="3.0">
  
    <xsl:output indent="yes" />

    <xsl:template match=".">
        <Root>
          <process>{.?process}</process>
          <title>{.?title}</title>
          <content>
            <xsl:sequence select="parse-xml-fragment(.?content)" />
          </content>
        </Root>
    </xsl:template>
    
</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <process>Test</process>
   <title>Json2XML_Conversion</title>
   <content>
      <div id="contents">
         <p class="Head">This need to process as title</p>
         <p class="para">This is child text</p>
      </div>
   </content>
</Root>

Json相关问答推荐

手动解开没有可编码的SON- Swift

从Json响应中为需要每个值的Post请求提取多个值

如何在数组抖动中按值分组

如何使用Aeson解码带有Unicode字符的JSON文件?

在MongoDB中检索某个月份和年份的JSON文档

属性错误:';ActivitiesClient';对象没有属性';base_url';

JOLT转换以基于对象属性过滤JSON数组

基于 JSON 字段的 Jolt 条件标志

将 std::可选值存储到 json 文件 C++

使用 jq 从字符串列表开始创建对象

如何在 Apps 脚本中循环遍历 JSON 响应

流编写器未写入 webapi 中的 JSON 文件

使用 Jolt 变换将平面 json 转换为具有多个数组的嵌套 Json

使用 ConvertFrom-Json 后,Powershell 访问 JSON 中的嵌套对象

将字符串映射到json对象的多种类型?

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

使用 Retrofit 解析动态密钥 Json 字符串

Retrofit2.0 得到 MalformedJsonException 而 json 似乎正确?

通过url获取json数据并在python中使用(simplejson)

如何在本地存储中存储对象数组?