Xslt 可以将输入参数转换为xml结构吗?
我需要将输入参数字符串(如“1-410000 54-420987 63-32000”)转换为xslt内部的结构(如下所示),以便在xslt中使用其数据:Xslt 可以将输入参数转换为xml结构吗?,xslt,xslt-1.0,Xslt,Xslt 1.0,我需要将输入参数字符串(如“1-410000 54-420987 63-32000”)转换为xslt内部的结构(如下所示),以便在xslt中使用其数据: <config:categories> <category> <value>410000</value> <label>1</label> </category> <category> <value>4
<config:categories>
<category>
<value>410000</value>
<label>1</label>
</category>
<category>
<value>420987</value>
<label>54</label>
</category>
<category>
<value>32000</value>
<label>63</label>
</category>
</config:categories>
410000
1.
420987
54
32000
63
附言。
如果在输入文档中找到左边的部分,是否还有其他选项来解析字符串,如“1-410000 54-420987 63-32000”,以便使用xslt中的数据来提取右边的部分(在“-”之后)?要从字符串生成XML,我想您必须使用Java。我不知道XSLT函数可以实现这一点 关于第二个问题: 有substring()、string-length()、substring-before()和substring-after()可能对您的请求有所帮助。 请参阅: 致以最良好的祝愿,
Peter要从字符串生成XML,我认为必须使用Java。我不知道XSLT函数可以实现这一点 关于第二个问题: 有substring()、string-length()、substring-before()和substring-after()可能对您的请求有所帮助。 请参阅: 致以最良好的祝愿,
彼得这一转变:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:config="some:config" exclude-result-prefixes="config">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pData" select="'1-410000 54-420987 63-32000'"/>
<xsl:template match="/*">
<config:categories>
<xsl:call-template name="gen"/>
</config:categories>
</xsl:template>
<xsl:template name="gen">
<xsl:param name="pGen" select="$pData"/>
<xsl:if test="$pGen">
<xsl:variable name="vChunk" select=
"substring-before(concat($pGen, ' '), ' ')"/>
<category>
<value><xsl:value-of select="substring-after($vChunk,'-')"/></value>
<label><xsl:value-of select="substring-before($vChunk,'-')"/></label>
</category>
<xsl:call-template name="gen">
<xsl:with-param name="pGen" select="substring-after($pGen, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<config:categories xmlns:config="some:config">
<category>
<value>410000</value>
<label>1</label>
</category>
<category>
<value>420987</value>
<label>54</label>
</category>
<category>
<value>32000</value>
<label>63</label>
</category>
</config:categories>
应用于任何XML文档(未使用)时,生成所需的正确结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:config="some:config" exclude-result-prefixes="config">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pData" select="'1-410000 54-420987 63-32000'"/>
<xsl:template match="/*">
<config:categories>
<xsl:call-template name="gen"/>
</config:categories>
</xsl:template>
<xsl:template name="gen">
<xsl:param name="pGen" select="$pData"/>
<xsl:if test="$pGen">
<xsl:variable name="vChunk" select=
"substring-before(concat($pGen, ' '), ' ')"/>
<category>
<value><xsl:value-of select="substring-after($vChunk,'-')"/></value>
<label><xsl:value-of select="substring-before($vChunk,'-')"/></label>
</category>
<xsl:call-template name="gen">
<xsl:with-param name="pGen" select="substring-after($pGen, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<config:categories xmlns:config="some:config">
<category>
<value>410000</value>
<label>1</label>
</category>
<category>
<value>420987</value>
<label>54</label>
</category>
<category>
<value>32000</value>
<label>63</label>
</category>
</config:categories>
410000
1.
420987
54
32000
63
说明:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:config="some:config" exclude-result-prefixes="config">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pData" select="'1-410000 54-420987 63-32000'"/>
<xsl:template match="/*">
<config:categories>
<xsl:call-template name="gen"/>
</config:categories>
</xsl:template>
<xsl:template name="gen">
<xsl:param name="pGen" select="$pData"/>
<xsl:if test="$pGen">
<xsl:variable name="vChunk" select=
"substring-before(concat($pGen, ' '), ' ')"/>
<category>
<value><xsl:value-of select="substring-after($vChunk,'-')"/></value>
<label><xsl:value-of select="substring-before($vChunk,'-')"/></label>
</category>
<xsl:call-template name="gen">
<xsl:with-param name="pGen" select="substring-after($pGen, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<config:categories xmlns:config="some:config">
<category>
<value>410000</value>
<label>1</label>
</category>
<category>
<value>420987</value>
<label>54</label>
</category>
<category>
<value>32000</value>
<label>63</label>
</category>
</config:categories>
正确使用和加上递归。此转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:config="some:config" exclude-result-prefixes="config">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pData" select="'1-410000 54-420987 63-32000'"/>
<xsl:template match="/*">
<config:categories>
<xsl:call-template name="gen"/>
</config:categories>
</xsl:template>
<xsl:template name="gen">
<xsl:param name="pGen" select="$pData"/>
<xsl:if test="$pGen">
<xsl:variable name="vChunk" select=
"substring-before(concat($pGen, ' '), ' ')"/>
<category>
<value><xsl:value-of select="substring-after($vChunk,'-')"/></value>
<label><xsl:value-of select="substring-before($vChunk,'-')"/></label>
</category>
<xsl:call-template name="gen">
<xsl:with-param name="pGen" select="substring-after($pGen, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<config:categories xmlns:config="some:config">
<category>
<value>410000</value>
<label>1</label>
</category>
<category>
<value>420987</value>
<label>54</label>
</category>
<category>
<value>32000</value>
<label>63</label>
</category>
</config:categories>
应用于任何XML文档(未使用)时,生成所需的正确结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:config="some:config" exclude-result-prefixes="config">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pData" select="'1-410000 54-420987 63-32000'"/>
<xsl:template match="/*">
<config:categories>
<xsl:call-template name="gen"/>
</config:categories>
</xsl:template>
<xsl:template name="gen">
<xsl:param name="pGen" select="$pData"/>
<xsl:if test="$pGen">
<xsl:variable name="vChunk" select=
"substring-before(concat($pGen, ' '), ' ')"/>
<category>
<value><xsl:value-of select="substring-after($vChunk,'-')"/></value>
<label><xsl:value-of select="substring-before($vChunk,'-')"/></label>
</category>
<xsl:call-template name="gen">
<xsl:with-param name="pGen" select="substring-after($pGen, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<config:categories xmlns:config="some:config">
<category>
<value>410000</value>
<label>1</label>
</category>
<category>
<value>420987</value>
<label>54</label>
</category>
<category>
<value>32000</value>
<label>63</label>
</category>
</config:categories>
410000
1.
420987
54
32000
63
说明:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:config="some:config" exclude-result-prefixes="config">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pData" select="'1-410000 54-420987 63-32000'"/>
<xsl:template match="/*">
<config:categories>
<xsl:call-template name="gen"/>
</config:categories>
</xsl:template>
<xsl:template name="gen">
<xsl:param name="pGen" select="$pData"/>
<xsl:if test="$pGen">
<xsl:variable name="vChunk" select=
"substring-before(concat($pGen, ' '), ' ')"/>
<category>
<value><xsl:value-of select="substring-after($vChunk,'-')"/></value>
<label><xsl:value-of select="substring-before($vChunk,'-')"/></label>
</category>
<xsl:call-template name="gen">
<xsl:with-param name="pGen" select="substring-after($pGen, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<config:categories xmlns:config="some:config">
<category>
<value>410000</value>
<label>1</label>
</category>
<category>
<value>420987</value>
<label>54</label>
</category>
<category>
<value>32000</value>
<label>63</label>
</category>
</config:categories>
正确使用和加上递归。正如Dimitre所示,在XSLT1.0中解析字符串相当麻烦。这是XSLT2.0远远优于它的一个领域。可以这样做:
<categories>
<xsl:for-each select="tokenize($param, '\s+')">
<category>
<label><xsl:value-of select="substring-after(., '-')"/></label>
<value><xsl:value-of select="substring-before(., '-')"/></value>
</category>
</xsl:for-each>
</categories>
当然,在XSLT 2.0中,您可以使用categories结构作为一级节点值,而不必使用node-set()扩展进入它。正如Dimitre所示,在XSLT 1.0中解析字符串相当麻烦。这是XSLT2.0远远优于它的一个领域。可以这样做:
<categories>
<xsl:for-each select="tokenize($param, '\s+')">
<category>
<label><xsl:value-of select="substring-after(., '-')"/></label>
<value><xsl:value-of select="substring-before(., '-')"/></value>
</category>
</xsl:for-each>
</categories>
当然,在XSLT 2.0中,您可以使用categories结构作为一级节点值,而不必使用node-set()扩展进入它。最好将文档作为参数传递。当然,也可以在XSLT中生成,但建议使用前者。最好将文档作为参数传递。当然,也可以在XSLT中生成该字符串,但建议使用前者。如果使用Java,则可以使用XSLT 2.0,并且在XSLT 2.0中将该字符串转换为XML比在Java中容易得多。如果使用Java,则可以使用XSLT 2.0,在XSLT2.0中将该字符串转换为XML要比在Java中容易得多。