在 xml 元素的值中获取 HTML 或 XHTML 文本
get HTML or XHTML text in Value of xml element
我的 xml 文档中有 projectDetails 节点,它是从 java jdom api 创建的,节点内的数据来自数据库。
问题在于当我将其添加到 <descriptionDetails />
元素并使用 Java 的转换 class 对其进行转换时,描述字段在 database.And 中存储为 html它是 escaping 所有 html 标签。
是否有可能像其他标签一样获取 Html 代码作为 descriptionDetails 的子项并且未转义。
<projectDetails label="label.projectDetails">
<descriptionDetails label="label.descriptionDetails">
<html>
<head></head>
<body>
<strong><strong> Tiny MCE Bold<br /><em>Tiny MCE Bold/Itellic</em><br /><span style="text-decoration: underline;"><em>Tiny MCE Bold/Itellic/Underlined</em></span><br /></strong></strong>
<div>
Lorem Ipsum&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
<br />
<br />
<span style="color: #ff0000;">printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset <span style="color: #ffffff; background-color: #808000;"><span style="background-color: #808000;">sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum,.</span><br /></span></span>
</div>
<h1>H1 heading</h1>
<h2>H1 heading</h2>
<h3>H1 heading</h3>
<h4>H1 heading</h4>
<h5>H1 heading</h5>
<h6>H1 heading<br /><br /><span style="font-size: 14pt;">font size 14</span></h6>
</body>
</html>
</descriptionDetails>
</projectDetails
private static String xmlAsString(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
return out.toString();
}
预期输出,
<projectDetails label="label.projectDetails">
<descriptionDetails label="label.descriptionDetails">
<html>
<head></head>
<body>
<strong><strong> Tiny MCE Bold<br /><em>Tiny MCE Bold/Itellic</em><br /><span style="text-decoration: underline;"><em>Tiny MCE Bold/Itellic/Underlined</em></span><br /></strong></strong>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
<br />
<br />
<span style="color: #ff0000;">printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset <span style="color: #ffffff; background-color: #808000;"><span style="background-color: #808000;">sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum,.</span><br /></span></span>
</div>
<h1>H1 heading</h1>
<h2>H1 heading</h2>
<h3>H1 heading</h3>
<h4>H1 heading</h4>
<h5>H1 heading</h5>
<h6>H1 heading<br /><br /><span style="font-size: 14pt;">font size 14</span></h6>
</body>
</html>
</descriptionDetails>
</projectDetails
这样的样式表中创建一个,而不是将默认的 Transformer 与 newInstance().newTransformer()
一起使用
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="descriptionDetails/text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
用作来源。
https://xsltfiddle.liberty-development.net/nc4NzR7
但是请注意,您的转义 HTML 包含例如一个实体引用
,然后会将您的输出变成格式不正确的内容 XML,因为该实体是XML.
中未预定义
我的 xml 文档中有 projectDetails 节点,它是从 java jdom api 创建的,节点内的数据来自数据库。
问题在于当我将其添加到 <descriptionDetails />
元素并使用 Java 的转换 class 对其进行转换时,描述字段在 database.And 中存储为 html它是 escaping 所有 html 标签。
是否有可能像其他标签一样获取 Html 代码作为 descriptionDetails 的子项并且未转义。
<projectDetails label="label.projectDetails">
<descriptionDetails label="label.descriptionDetails">
<html>
<head></head>
<body>
<strong><strong> Tiny MCE Bold<br /><em>Tiny MCE Bold/Itellic</em><br /><span style="text-decoration: underline;"><em>Tiny MCE Bold/Itellic/Underlined</em></span><br /></strong></strong>
<div>
Lorem Ipsum&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
<br />
<br />
<span style="color: #ff0000;">printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset <span style="color: #ffffff; background-color: #808000;"><span style="background-color: #808000;">sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum,.</span><br /></span></span>
</div>
<h1>H1 heading</h1>
<h2>H1 heading</h2>
<h3>H1 heading</h3>
<h4>H1 heading</h4>
<h5>H1 heading</h5>
<h6>H1 heading<br /><br /><span style="font-size: 14pt;">font size 14</span></h6>
</body>
</html>
</descriptionDetails>
</projectDetails
private static String xmlAsString(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
return out.toString();
}
预期输出,
<projectDetails label="label.projectDetails">
<descriptionDetails label="label.descriptionDetails">
<html>
<head></head>
<body>
<strong><strong> Tiny MCE Bold<br /><em>Tiny MCE Bold/Itellic</em><br /><span style="text-decoration: underline;"><em>Tiny MCE Bold/Itellic/Underlined</em></span><br /></strong></strong>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
<br />
<br />
<span style="color: #ff0000;">printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset <span style="color: #ffffff; background-color: #808000;"><span style="background-color: #808000;">sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum,.</span><br /></span></span>
</div>
<h1>H1 heading</h1>
<h2>H1 heading</h2>
<h3>H1 heading</h3>
<h4>H1 heading</h4>
<h5>H1 heading</h5>
<h6>H1 heading<br /><br /><span style="font-size: 14pt;">font size 14</span></h6>
</body>
</html>
</descriptionDetails>
</projectDetails
newInstance().newTransformer()
一起使用
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="descriptionDetails/text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
用作来源。
https://xsltfiddle.liberty-development.net/nc4NzR7
但是请注意,您的转义 HTML 包含例如一个实体引用
,然后会将您的输出变成格式不正确的内容 XML,因为该实体是XML.