XSLT 使用正则表达式转换值
XSLT transform a value with regex
我在 windows xp 上使用带有 xslt3.0 的 Saxon 9.8 将 xml 数据库转换为普通文本文件,每行一个数据库条目。
但我需要使用正则表达式和条件处理一个值,当我添加以下代码部分时:
<xsl:analyze-string select="sbtime/@stmerid"
regex="([hm]{{1}})([0-9]{{1,2}})([ew]{{1}})([0-9]{{0,2}})">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(3) = 'e'">
<xsl:text>+</xsl:text>
</xsl:when>
<xsl:when test="regex-group(3) = 'w'">
<xsl:text>-</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>+</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="regex-group(1) = 'h'">
<xsl:number value="regex-group(2)" format="01"/>
</xsl:when>
<xsl:when test="regex-group(1) = 'm'">
<xsl:text>00:</xsl:text>
<xsl:number value="regex-group(2)" format="01"/>
<xsl:text>:</xsl:text>
<xsl:number value="regex-group(4)" format="01"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>+1</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
</xsl:analyze-string>
当我添加这部分时,我得到以下错误代码:
Transform.exe" -s:db_export_sample.xml -xsl:dbx_dma_zet01.xsl -o:output.txt
Exception non gérée : System.TypeInitializationException: Une exception a été le
vée par l'initialiseur de type pour 'net.sf.saxon.expr.number.NumberFormatter'.
---> System.TypeInitializationException: Une exception a été levée par l'initial
iseur de type pour 'net.sf.saxon.regex.charclass.Categories'. ---> java.lang.Run
timeException: Unable to read categories.xml file
à net.sf.saxon.regex.charclass.Categories.build()
à net.sf.saxon.regex.charclass.Categories.getCategory(String cat)
à net.sf.saxon.regex.charclass.Categories..cctor()
--- Fin de la trace de la pile d'exception interne ---
à net.sf.saxon.regex.charclass.Categories.getCategory(String cat)
à net.sf.saxon.expr.number.NumberFormatter..cctor()
--- Fin de la trace de la pile d'exception interne ---
à net.sf.saxon.expr.number.NumberFormatter..ctor()
à net.sf.saxon.style.XSLNumber.prepareAttributes()
à net.sf.saxon.style.StyleElement.processAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.XSLTemplate.processAllAttributes()
à net.sf.saxon.style.PrincipalStylesheetModule.processAllAttributes()
à net.sf.saxon.style.PrincipalStylesheetModule.preprocess()
à net.sf.saxon.style.Compilation.compilePackage(Source source)
à net.sf.saxon.style.StylesheetModule.loadStylesheet(Source styleSource, Comp
ilation compilation)
à net.sf.saxon.style.Compilation.compileSingletonPackage(Configuration config
, CompilerInfo compilerInfo, Source source)
à net.sf.saxon.s9api.XsltCompiler.compile(Source source)
à net.sf.saxon.Transform.doTransform(String[] args, String command)
à Saxon.Cmd.DotNetTransform.Main(String[] args)
顺便说一句,抱歉,代码中的法语可能无法理解。
我不习惯 xslt,但根据 xslt 和 saxon 文档,它似乎是正确的。
此致。
这是错误 3283
https://saxonica.plan.io/issues/3283
在维护版本中很快得到修复。
我在 windows xp 上使用带有 xslt3.0 的 Saxon 9.8 将 xml 数据库转换为普通文本文件,每行一个数据库条目。 但我需要使用正则表达式和条件处理一个值,当我添加以下代码部分时:
<xsl:analyze-string select="sbtime/@stmerid"
regex="([hm]{{1}})([0-9]{{1,2}})([ew]{{1}})([0-9]{{0,2}})">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(3) = 'e'">
<xsl:text>+</xsl:text>
</xsl:when>
<xsl:when test="regex-group(3) = 'w'">
<xsl:text>-</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>+</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="regex-group(1) = 'h'">
<xsl:number value="regex-group(2)" format="01"/>
</xsl:when>
<xsl:when test="regex-group(1) = 'm'">
<xsl:text>00:</xsl:text>
<xsl:number value="regex-group(2)" format="01"/>
<xsl:text>:</xsl:text>
<xsl:number value="regex-group(4)" format="01"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>+1</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
</xsl:analyze-string>
当我添加这部分时,我得到以下错误代码:
Transform.exe" -s:db_export_sample.xml -xsl:dbx_dma_zet01.xsl -o:output.txt
Exception non gérée : System.TypeInitializationException: Une exception a été le
vée par l'initialiseur de type pour 'net.sf.saxon.expr.number.NumberFormatter'.
---> System.TypeInitializationException: Une exception a été levée par l'initial
iseur de type pour 'net.sf.saxon.regex.charclass.Categories'. ---> java.lang.Run
timeException: Unable to read categories.xml file
à net.sf.saxon.regex.charclass.Categories.build()
à net.sf.saxon.regex.charclass.Categories.getCategory(String cat)
à net.sf.saxon.regex.charclass.Categories..cctor()
--- Fin de la trace de la pile d'exception interne ---
à net.sf.saxon.regex.charclass.Categories.getCategory(String cat)
à net.sf.saxon.expr.number.NumberFormatter..cctor()
--- Fin de la trace de la pile d'exception interne ---
à net.sf.saxon.expr.number.NumberFormatter..ctor()
à net.sf.saxon.style.XSLNumber.prepareAttributes()
à net.sf.saxon.style.StyleElement.processAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.StyleElement.processAllAttributes()
à net.sf.saxon.style.XSLTemplate.processAllAttributes()
à net.sf.saxon.style.PrincipalStylesheetModule.processAllAttributes()
à net.sf.saxon.style.PrincipalStylesheetModule.preprocess()
à net.sf.saxon.style.Compilation.compilePackage(Source source)
à net.sf.saxon.style.StylesheetModule.loadStylesheet(Source styleSource, Comp
ilation compilation)
à net.sf.saxon.style.Compilation.compileSingletonPackage(Configuration config
, CompilerInfo compilerInfo, Source source)
à net.sf.saxon.s9api.XsltCompiler.compile(Source source)
à net.sf.saxon.Transform.doTransform(String[] args, String command)
à Saxon.Cmd.DotNetTransform.Main(String[] args)
顺便说一句,抱歉,代码中的法语可能无法理解。 我不习惯 xslt,但根据 xslt 和 saxon 文档,它似乎是正确的。
此致。
这是错误 3283
https://saxonica.plan.io/issues/3283
在维护版本中很快得到修复。