Xml 如何解析分隔字符串?
我有一个以下格式的分隔字符串:Xml 如何解析分隔字符串?,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我有一个以下格式的分隔字符串: 1^^2^^3^^4^^5^^||1^^2^^3^^4^^5^^|| 其中^^是列分隔符,| |是行分隔符。我需要能够遍历每一行,然后遍历每一列,并将值存储在不同的变量中。我想知道如何使用XSL 1.0实现这一点。任何形式的帮助都将不胜感激下面是一个使用EXSLT的str:split的基于模板的实现的示例: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transfo
1^^2^^3^^4^^5^^||1^^2^^3^^4^^5^^||
其中^^是列分隔符,| |是行分隔符。我需要能够遍历每一行,然后遍历每一列,并将值存储在不同的变量中。我想知道如何使用XSL 1.0实现这一点。任何形式的帮助都将不胜感激下面是一个使用EXSLT的
str:split的基于模板的实现的示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="exsl str">
<xsl:import href="http://exslt.org/str/functions/split/str.split.template.xsl"/>
<xsl:output indent="yes"/>
<xsl:template match="data">
<xsl:variable name="rows-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'||'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="rows" select="exsl:node-set($rows-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$rows">
<row>
<xsl:variable name="cols-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'^^'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="cols" select="exsl:node-set($cols-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$cols">
<col><xsl:value-of select="."/></col>
</xsl:for-each>
</row>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>a</col>
<col>b</col>
<col>c</col>
<col>d</col>
<col>c</col>
</row>
1.
2.
3.
4.
5.
1.
2.
3.
4.
5.
A.
B
C
D
C
对于输入样本
<root>
<data>1^^2^^3^^4^^5^^||1^^2^^3^^4^^5^^||a^^b^^c^^d^^c</data>
</root>
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ a ^ b ^ c ^ d ^ c
下面是一个使用EXSLT的str:split的基于模板的实现的示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="exsl str">
<xsl:import href="http://exslt.org/str/functions/split/str.split.template.xsl"/>
<xsl:output indent="yes"/>
<xsl:template match="data">
<xsl:variable name="rows-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'||'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="rows" select="exsl:node-set($rows-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$rows">
<row>
<xsl:variable name="cols-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'^^'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="cols" select="exsl:node-set($cols-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$cols">
<col><xsl:value-of select="."/></col>
</xsl:for-each>
</row>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>a</col>
<col>b</col>
<col>c</col>
<col>d</col>
<col>c</col>
</row>
1.
2.
3.
4.
5.
1.
2.
3.
4.
5.
A.
B
C
D
C
对于输入样本
<root>
<data>1^^2^^3^^4^^5^^||1^^2^^3^^4^^5^^||a^^b^^c^^d^^c</data>
</root>
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ a ^ b ^ c ^ d ^ c
下面是一个使用EXSLT的str:split的基于模板的实现的示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="exsl str">
<xsl:import href="http://exslt.org/str/functions/split/str.split.template.xsl"/>
<xsl:output indent="yes"/>
<xsl:template match="data">
<xsl:variable name="rows-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'||'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="rows" select="exsl:node-set($rows-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$rows">
<row>
<xsl:variable name="cols-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'^^'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="cols" select="exsl:node-set($cols-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$cols">
<col><xsl:value-of select="."/></col>
</xsl:for-each>
</row>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>a</col>
<col>b</col>
<col>c</col>
<col>d</col>
<col>c</col>
</row>
1.
2.
3.
4.
5.
1.
2.
3.
4.
5.
A.
B
C
D
C
对于输入样本
<root>
<data>1^^2^^3^^4^^5^^||1^^2^^3^^4^^5^^||a^^b^^c^^d^^c</data>
</root>
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ a ^ b ^ c ^ d ^ c
下面是一个使用EXSLT的str:split的基于模板的实现的示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="exsl str">
<xsl:import href="http://exslt.org/str/functions/split/str.split.template.xsl"/>
<xsl:output indent="yes"/>
<xsl:template match="data">
<xsl:variable name="rows-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'||'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="rows" select="exsl:node-set($rows-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$rows">
<row>
<xsl:variable name="cols-rtf">
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pattern" select="'^^'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="cols" select="exsl:node-set($cols-rtf)/token[normalize-space()]"/>
<xsl:for-each select="$cols">
<col><xsl:value-of select="."/></col>
</xsl:for-each>
</row>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>1</col>
<col>2</col>
<col>3</col>
<col>4</col>
<col>5</col>
</row>
<row>
<col>a</col>
<col>b</col>
<col>c</col>
<col>d</col>
<col>c</col>
</row>
1.
2.
3.
4.
5.
1.
2.
3.
4.
5.
A.
B
C
D
C
对于输入样本
<root>
<data>1^^2^^3^^4^^5^^||1^^2^^3^^4^^5^^||a^^b^^c^^d^^c</data>
</root>
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ a ^ b ^ c ^ d ^ c
只要对调用str:split
模板返回的结果树片段应用exsl:node set
,就可以使用或类似的解决方案。只要对调用str:split
模板返回的结果树片段应用exsl:node set
,就可以使用或类似的解决方案。您可以只要对调用str:split
模板返回的结果树片段应用exsl:node set
,就可以使用或类似的解决方案。只要对调用str:split
模板返回的结果树片段应用exsl:node set
,就可以使用或类似的解决方案。感谢回答。我将如何解析这些数据?还有,我将输入字符串放在何处以及如何放置?我假设输入字符串是输入XML文档中某个元素的值,因为XSLT通常就是这样工作的。明白了。谢谢你,谢谢你!感谢你的回答。我将如何解析这些数据?还有,我将输入字符串放在何处以及如何放置?我假设输入字符串是输入XML文档中某个元素的值,因为XSLT通常就是这样工作的。明白了。谢谢你,谢谢你!感谢你的回答。我将如何解析这些数据?还有,我将输入字符串放在何处以及如何放置?我假设输入字符串是输入XML文档中某个元素的值,因为XSLT通常就是这样工作的。明白了。谢谢你,谢谢你!感谢你的回答。我将如何解析这些数据?还有,我将输入字符串放在何处以及如何放置?我假设输入字符串是输入XML文档中某个元素的值,因为XSLT通常就是这样工作的。明白了。谢谢你,谢谢你!