【问题标题】:xslt2.0 unparsed-text with regexp带有正则表达式的 xslt 2.0 未解析文本
【发布时间】:2013-06-17 10:57:17
【问题描述】:

我有纯文本: 输入文字

ExternalEvent(GpiIn2: LOW->HI) - AirAction(Play)
AirLog
<log date="2012-07-22" Audio="123.wav" />
AirLog

当我使用代码 xslt 2.0 转换时:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:variable name="vText" select=
 "replace(unparsed-text('file:///c:/123.log'),'\r','')"/>

 <xsl:template match="/">
  <document>
      <xsl:analyze-string select="$vText" regex="'\&lt;'">
         <xsl:non-matching-substring><xsl:sequence select="."/></xsl:non-matching-substring>
      </xsl:analyze-string>
  </document>
 </xsl:template>
</xsl:stylesheet>

我得到了 xml:

<document>
    ExternalEvent(GpiIn2: LOW-&gt;HI) - AirAction(Play)
    AirLog
    &lt;log date="2012-07-22" Audio="123.wav" /&gt;
    AirLog
</document>

任何人都可以告诉我,我需要添加到正则表达式以获得格式良好的 XML,喜欢的是:

<document>
      <log date="2012-07-22" Audio="123.wav" >
</document>

【问题讨论】:

    标签: xml regex xslt-2.0 plaintext


    【解决方案1】:

    使用 XSLT 2.0 处理器,如 XmlPrime 或 Saxon,它们已经支持 W3C XPath 3.0 中的功能,如 parse-xml-fragment,您可以简单地做

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output indent="yes"/>
    
    <xsl:param name="url1" select="'file:///c:/123.log'"/>
    
    <xsl:template match="/">
      <document>
        <xsl:copy-of select="parse-xml-fragment(unparsed-text($url1))//log"/>
      </document>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 2016-04-27
      • 1970-01-01
      • 2018-01-28
      • 2018-02-09
      • 2015-10-10
      相关资源
      最近更新 更多