0

我有纯文本: 输入文本

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

1 回答 1

0

使用 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>
于 2013-06-19T11:28:12.880 回答