使用内联样式表显示HTML中的XML [英] show XML in HTML with inline stylesheet

查看:225
本文介绍了使用内联样式表显示HTML中的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它不是重复的,问题不是在html中显示xml,而是为单行显示xml偶尔的样式表。



我在寻找一个解决方案来显示xml在html页面与inline css。
背景:输入数据是一个xml文件,将与java中的另一个xml文件进行比较。比较由org.custommonkey.xmlunit处理。
现在我喜欢通过xslt和xpathes从xunit的比较结果转换xml。



输入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>
< /返回>
< / OUTPUT>
< / MATDETAIL>
< / ROOT>

XUnit差异结果XPath
/ ROOT [1] / MATDETAIL [1] / OUTPUT [ 1] / GENERAL [1] / CHANGED_BY [1] / text()[1]



现在我将通过xslt >

my xsl显示为




 < xsl:template match =node()| @ *> 
< xsl:copy>
< xsl:apply-templates select =node()| @ *mode =unes​​cape/>
< / 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;颜色:红色< / xsl:attribute>
< xsl:copy>
< xsl:value-of select =current()/>
< / xsl:copy>
< xsl:text>& lt; ==预期:dasda< / xsl:text>
< / xsl:element>
< / xsl:template>
< / xsl:stylesheet>

这将给我这个结果

 < 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(使用样式表)。
我没有标签,所以看到xml strcuture是非常复杂
也得到xml结构,但没有样式表。



我喜欢以显示结果,从两个文件的xunit比较,在web服务和这也是彩色,所以你可以很容易地看到哪些元素有变化或不正确。



问题是显示完整的xml +,这似乎是真正的问题,用样式表为条目着色。我可以显示xml,但没有内联样式表,或者我可以显示样式文本,但不能同时显示。



我已经尝试替换lt和gt内部xslt但失败。
我试图通过示例java替换rseult中的lt和gt。所以每个lt和gt都是html编码的标签。但结果显示非常丑陋。我的同事意味着我们不能以任何方式使用它。
我试图通过css>定义每个标签的样式xml。
但是结构和字段是动态充满的,所以我不能为每个标签或字段定义css。另一方面每个字段可以正确或错误,还取决于xml文件喜欢比较。所以字段changedby是在第一次比较正确,但在另一个比较它的错误



不幸的是,替代实现不是一个选项。我必须用我能用的方法做这个。 (Java,XML& XSL,JS,HTML,CSS)。



我希望得到好的想法来解决这个问题。



我想提前感谢你。

解决方案

我不确定要了解你的要求。
如果要在HTML中显示XML,可以将其插入PRE标签中:< pre>< ROOT> ...< / ROOT>< / pre> / code>



如果你想显示XML,因为它可以显示一个HTML / XML编辑器,你可以使用Ace: https://ace.c9.io/#nav=about



其他选项但未尝试: https://highlightjs.org


It's not an duplicate, the problem ist not show an xml in html, rather to show xml with occasional stylesheets for single lines.

i'm looking since hourse for an solution to show xml in a html page with inline css. Background: Input data is an xml file, what will be compared with another xml file in java. The comparison is processed by org.custommonkey.xmlunit. Now i like to transform the xml via xslt and the xpathes from the comparison result of xunit.

Input XML (Multilevel, but for this example very simplified, the Field changed by is exemplary for all other fields)

<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>

XUnit Diff Result XPath /ROOT[1]/MATDETAIL[1]/OUTPUT[1]/GENERAL[1]/CHANGED_BY[1]/text()[1]

now i will be perfom an translation via xslt (already existing)

my xsl shows like that

<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>

that will give me this result

<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>

But i'm not able to show this xml in html correctly (with stylesheets). Either i get no tags, so to see the xml strcuture is very complicated also i get the xml structure but without stylesheets.

I like to show the result, from comparison with xunit of the two files, in a webservice and this also colored, so you can easy see which elements have change or are incorrectly.

The Problem is to show the full xml + and that seems to be the real problem to color the entries with stylesheets. I can show the xml, but without inline stylesheets, or i can show the styled text but not both.

I already tried to replace lt and gt inside xslt but failed. I tried to replace lt and gt in the rseult via example java. so every lt and gt are html coded tags. but the result shows very ugly. My colleague has meant that we can not use it in any way. I tried to style the xml via css > definiton per tag. But the the structure and fields are full dynamicly, so i cant define css for every tag or field. On the other hand every field can be correct or wrong, depends also on the xml file like to compare. So the field changedby is at the first comparison correct, but on an other comparison its wrong

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 would like to thank you in advance.

解决方案

I'm not sure to understand your request. If you want to display XML in HTML, you can insert it in the PRE tags : <pre><ROOT>...</ROOT></pre>

If you want to display XML as it could be displayed un an HTML/XML editor you can use Ace : https://ace.c9.io/#nav=about

Other option but not tried : https://highlightjs.org

这篇关于使用内联样式表显示HTML中的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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