我在数据中得到像<p>这样的特殊字符,并且需要像行一样格式化

问题描述 投票:0回答:1
xml xslt xslt-2.0
1个回答
0
投票

考虑以下简化示例:

XML

<PSQ2>&lt;p>Going beyond working hours to meet release timeline and other priority stuff.&lt;/p>&lt;p>Skill set wise, I feel he is technically very well sound. Behavior wise, he is very friendly, make people feel like he is one of a family member. He advices people in both personal and professional ways. So far I didn&amp;#39;t feel that he keeps double thoughts.&lt;/p>&lt;p>&lt;span>He has shown good ability to analyze issues, make sound decisions and overcome problems. Always trying to improve code quality. &lt;/span>&lt;/p></PSQ2>

XSLT 3.0

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

<xsl:template match="PSQ2">
    <result>
        <xsl:value-of select="parse-xml-fragment(.)/p" />
    </result>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<result>Going beyond working hours to meet release timeline and other priority stuff. Skill set wise, I feel he is technically very well sound. Behavior wise, he is very friendly, make people feel like he is one of a family member. He advices people in both personal and professional ways. So far I didn't feel that he keeps double thoughts. He has shown good ability to analyze issues, make sound decisions and overcome problems. Always trying to improve code quality. </result>

parse-xml-fragment()
功能需要支持XSLT 3.0的处理器。如果没有这个,从转义标记中提取文本将变得更加困难


补充:

将转义标记解析为 XML 后,您可以按照您喜欢的任何方式将其处理为 XML - 例如:

<xsl:template match="PSQ2">
    <result>
        <xsl:for-each select="parse-xml-fragment(.)/p">
            <xsl:number format="1. "/>
            <xsl:value-of select="." />
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </result>
</xsl:template>

会产生:

<result>1. Going beyond working hours to meet release timeline and other priority stuff.
2. Skill set wise, I feel he is technically very well sound. Behavior wise, he is very friendly, make people feel like he is one of a family member. He advices people in both personal and professional ways. So far I didn't feel that he keeps double thoughts.
3. He has shown good ability to analyze issues, make sound decisions and overcome problems. Always trying to improve code quality. 
</result>
© www.soinside.com 2019 - 2024. All rights reserved.