在完整的 XML 结构中为偶尔的线条着色并以 HTML 显示 [英] Coloring occasional lines in full XML Structure and show in HTML

查看:28
本文介绍了在完整的 XML 结构中为偶尔的线条着色并以 HTML 显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个问题被标记为重复,但不是重复的

使用内联样式表在 HTML 中显示 XML

我希望这个问题不会被立即标记为重复,只是因为其中一位版主已经阅读了前两句话而忽略了其余部分.

问题不是在 HTML 中显示 XML 结构,而是显示完整的动态 XML 结构,带有所有标签和偶尔的彩色线条.结构和内部字段是动态满的,每个字段都可以正确或错误,这取决于要比较的xml文件.所以一个字段在第一次比较时是正确的,但在另一次比较中却是错误的.XML 的字段和结构可能因一种比较而异.从昨天开始,我一直在寻找针对此问题的相应专业解决方案.后台进程:比较不同的xml文件,通过java中的soa微服务.比较由 org.custommonkey.xmlunit 进行.结果必须是一个 html 弹出窗口,它显示了用彩色线条标记的差异.

The Problem is not to show an XML Structure in HTML, rather to show a full dynamically XML structure, with all tags and occasional colored lines. The structure and interior fields are full dynamically and every field can be correct or wrong, depending on the xml file like to compare. So a field is at the first comparison correct, but on another comparison it’s wrong. The fields and structure of XML can vary greatly from one comparison to another. I’m looking since yesterday for a corresponding and professional solution for this problem. Background process: comparison different xml files, via soa microservices in java. The comparison is made by org.custommonkey.xmlunit. The Result have to be an html popup, what shows me differences marked by colored lines.

示例输出 XUnit Diff 结果 XPath

Example Output XUnit Diff Result XPath

/ROOT[1]/MATDETAIL[1]/OUTPUT[1]/GENERAL[1]/CHANGED_BY[1]/text()[1]

通过 xslt 和 xunit diff 结果信息转换源 xml.

Transform the source xml via xslt and the xunit diff result informations.

示例输入 XML

<ROOT>    
      <MATDETAIL>
            <OUTPUT>
                  <GENERAL>
                        <CREATED_ON/>
                        <CREATED_BY>ORIGINAL USER</CREATED_BY>
                        <LAST_CHNGE/>
                        <CHANGED_BY>NEW USER</CHANGED_BY>
                  </GENERAL>
                  <RETURN>
                        <TYPE>S</TYPE>
                        <MESSAGE/>
                        <LOG_NO/>
                        <LOG_MSG_NO>000000</LOG_MSG_NO>
                  </RETURN>
            </OUTPUT>
      </MATDETAIL>  
</ROOT>

XSL 转换示例

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

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="unescape"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/ROOT[1]/MATDETAIL[1]/OUTPUT[1]/GENERAL[1]/CHANGED_BY[1]">

    <xsl:element name = "span">
        <xsl:attribute name="style">font-weight:bold; color:red </xsl:attribute>
        <xsl:copy>
            <xsl:value-of select = "current()" />
        </xsl:copy>
        <xsl:text>&lt;== Expected: dasda</xsl:text>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

XSL 转换的示例结果

Example Result of XSL Transformation

<ROOT>    
      <MATDETAIL>
            <OUTPUT>
                  <GENERAL>
                        <CREATED_ON/>
                        <CREATED_BY>ORIGINAL USER</CREATED_BY>
                        <LAST_CHNGE/>
                        <span style="font-weight:bold; color:red "><CHANGED_BY>NEW USER</CHANGED_BY>&lt;== Expected: ORIGINAL USER</span>
                  </GENERAL>
                  <RETURN>
                        <TYPE>S</TYPE>
                        <MESSAGE/>
                        <LOG_NO/>
                        <LOG_MSG_NO>000000</LOG_MSG_NO>
                  </RETURN>
            </OUTPUT>
      </MATDETAIL>  
</ROOT>

我无法在 html 中显示此 xml 结构,带有(所有)标签,正确地AND着色.要么我没有标签,所以只能看到 XML 中的原始数据,没有标签,但线条是彩色的.或者我得到包含所有数据的 xml 结构,但没有着色.我试图替换xslt中的lt和gt字符,但失败了,或者在java中转换后,这个结果显示非常难看.我的同事的意思是我们不能以任何方式使用它.因为 XML 结构每次都可以完全动态地不同等等,所以我不能使用 css 和标签定义来设置 xml 的样式.不幸的是,替代实现不是一种选择.我必须用我可用的手段以某种方式做到这一点.(Java、XML 和 XSL、JS、HTML、CSS).

I’m not able to show this xml structure in html, with (all) tags, correctly AND colored. Either I’ve get no tags, so there are only the raw data in XML to see, without tags, but the lines are colored. Or I get the xml structure with all data, but not colored. I tried to replace the lt and gt chars inside xslt, but failed, or after transformation in java, this result shows very ugly. My colleague has meant that we can not use it in any way. Because the XML Structur can be every time different and so on fully dynamically, I can not style the xml with css and tag definition. Unfortunately, alternative implementations are not an option. I have to do this somehow with the means available to me. (Java, XML & XSL, JS, HTML, CSS).

我希望有好的想法来解决这个问题.

I hope to get good ideas to solute this.

我想提前感谢您.

推荐答案

我希望我可以通过以下尝试解决您的问题.

I hope i can solve your issue with following try.

我.输入:

<ROOT baum="baum">    
    <MATDETAIL>
        <OUTPUT>
            <GENERAL>
                <CREATED_ON/>
                <CREATED_BY>ORIGINAL USER</CREATED_BY>
                <LAST_CHNGE/>
                <CHANGED_BY>NEW USER</CHANGED_BY>
            </GENERAL>
            <RETURN>
                <TYPE>S</TYPE>
                <MESSAGE/>
                <LOG_NO/>
                <LOG_MSG_NO>000000</LOG_MSG_NO>
            </RETURN>
        </OUTPUT>
    </MATDETAIL>  
</ROOT>

二.样式表 (XSLT 1.0):

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

    <xsl:output omit-xml-declaration="yes"/>

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

    <xsl:template match="CHANGED_BY">
        <span style="color:red;">
            <xsl:apply-templates select="." mode="serialize"/>
        </span>
    </xsl:template>

    <xsl:template match="*">
        <xsl:apply-templates select="." mode="serialize"/>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:apply-templates select="." mode="serialize"/>
    </xsl:template>

    <xsl:template match="*" mode="serialize">
        <xsl:value-of select="concat('&lt;', name())"/>
        <xsl:apply-templates select="@*" />
        <xsl:choose>
            <xsl:when test="node()">
                <xsl:text>&gt;</xsl:text>
                <xsl:apply-templates />
                <xsl:value-of select="concat('&lt;', name(), '&gt;')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text> /&gt;</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="@*" mode="serialize">
        <xsl:value-of select="concat(' ', name(), '=&quot;', ., '&quot;')"/>
    </xsl:template>

    <xsl:template match="text()" mode="serialize">
        <xsl:value-of select="."/>
    </xsl:template>

</xsl:stylesheet>

III:输出:

&lt;ROOT baum="baum"&gt;    
    &lt;MATDETAIL&gt;
        &lt;OUTPUT&gt;
            &lt;GENERAL&gt;
                &lt;CREATED_ON /&gt;
                &lt;CREATED_BY&gt;ORIGINAL USER&lt;/CREATED_BY&gt;
                &lt;LAST_CHNGE /&gt;
                <span style="color:red;">&lt;CHANGED_BY&gt;NEW USER&lt;/CHANGED_BY&gt;</span>
            &lt;/GENERAL&gt;
            &lt;RETURN&gt;
                &lt;TYPE&gt;S&lt;/TYPE&gt;
                &lt;MESSAGE /&gt;
                &lt;LOG_NO /&gt;
                &lt;LOG_MSG_NO&gt;000000&lt;/LOG_MSG_NO&gt;
            &lt;/RETURN&gt;
        &lt;/OUTPUT&gt;
    &lt;/MATDETAIL&gt;  
&lt;/ROOT&gt;

四.说明:

每当应用 mode="serialize" 时,上下文都会被转义.请参阅 CHANGED_BY 的示例以使用 HTML 标签进行格式化.xml 结构已完全转义,因此浏览器将其显示为字符串而不是标签.

Whenever the mode="serialize" is applied, the context is escaped. See example for CHANGED_BY to format with HTML-Tags. The xml structure is fully escaped so the browser shows it like a string instead of tags.

我真的希望它能解决你的问题

这篇关于在完整的 XML 结构中为偶尔的线条着色并以 HTML 显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆