我有一个需要转换为json的xml。除了使用base64多行,我基本上都没问题
<file>TU0...AAA
FOO...BCD
FOO...012
FOO...ZYX</file>
无法使用json多行,只能在1行中将其重写为
"file":"TU0...AAA\nFOO...BCD\nFOO...012\nFOO...ZYX"
用“真实的”两个字符的字符串“ \ n”来连接每一行。
我可以在xslt 1.0中做到吗?
我知道我可以使用翻译,但这仅用于一个字符。我会尝试
translate(.,' ',' ')
这将用空格替换返回值,也许这不会破坏json的base64解码。
但是,如果我想以“正确的方式”进行操作,我想我需要自定义功能。就我而言,回报似乎是“”。但是如果有人提供了适用于所有组合的解决方案()那就太好了。
我的主要目标是chrome网络浏览器,但在所有浏览器中都能正常运行。
normalize-space($string)
功能,例如:<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xd"
version="1.0">
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> Apr 22, 2020</xd:p>
<xd:p><xd:b>Author:</xd:b> bwb</xd:p>
<xd:p>generates a normalized text output of the file element</xd:p>
</xd:desc>
</xd:doc>
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="file"/>
</xsl:template>
<xsl:template match="file">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>
然后您仍然可以用其他东西替换空白(forJSON可能是,
)如果您确实想要
\n
,则可以尝试以下样式表:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" exclude-result-prefixes="xs math xd" version="1.0"> <xd:doc scope="stylesheet"> <xd:desc> <xd:p><xd:b>Created on:</xd:b> Apr 22, 2020</xd:p> <xd:p><xd:b>Author:</xd:b> bwb</xd:p> <xd:p>Override default text() template by adding a search and replace funtionality</xd:p> </xd:desc> </xd:doc> <xsl:output method="text"/> <xd:doc scope="component"> <xd:desc>The string that should be searched and replaced by $param-replaceString</xd:desc> </xd:doc> <xsl:param name="param-searchString" select="' '"/><!-- actually you also wnat to replace the whitespaces, that's why the searchString looks so strange --> <xd:doc> <xd:desc>The string that replace any occurence of $param-searchString</xd:desc> </xd:doc> <xsl:param name="param-replaceString" select="'\n'"/> <xd:doc scope="component"> <xd:desc>Override for default text() template testing for $param-searchString presence and calling replace template</xd:desc> </xd:doc> <xsl:template match="text()"> <xsl:choose> <xsl:when test="contains(., $param-searchString)"> <xsl:call-template name="replace"> <xsl:with-param name="InputString" select="."/> <xsl:with-param name="searchString" select="$param-searchString"/> <xsl:with-param name="replaceString" select="$param-replaceString"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="replace"> <xsl:param name="InputString"/> <xsl:param name="searchString"/> <xsl:param name="replaceString"/> <xsl:choose> <xsl:when test="contains($InputString, $searchString)"> <xsl:variable name="token-before-first-match" select="substring-before($InputString, $searchString)"/> <xsl:variable name="token-after-first-match" select="substring-after(., concat($token-before-first-match, $searchString))"/> <xsl:value-of select="concat($token-before-first-match, $replaceString)"/> <xsl:choose> <xsl:when test="contains($token-after-first-match, $searchString)"> <xsl:call-template name="replace"> <xsl:with-param name="InputString" select="$token-after-first-match"/> <xsl:with-param name="searchString" select="$searchString"/> <xsl:with-param name="replaceString" select="$replaceString"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$token-after-first-match"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:value-of select="$InputString"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>