请帮助:尝试通过删除前导零和空格来转换XML;下面的XSLT对我不起作用:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="xalan://org.apache.commons.lang.StringUtils"
exclude-result-prefixes="str">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="h1">
<h1>
<xsl:variable name="leadingZeroRemoved">
<xsl:call-template name="removeLeadingZero">
<xsl:with-param name="text" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="leadingSpaceRemoved">
<xsl:call-template name="removeLeadingSpace">
<xsl:with-param name="text" select="$leadingZeroRemoved" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="trailingSpaceRemoved">
<xsl:call-template name="removeTrailingSpace">
<xsl:with-param name="text" select="$leadingSpaceRemoved" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$trailingSpaceRemoved" />
</h1>
</xsl:template>
<xsl:template name="removeLeadingZero">
<xsl:param name="text" />
<xsl:variable name="h1" select="$text" />
<xsl:choose>
<xsl:when test="starts-with($text,'0')">
<xsl:call-template name="removeLeadingZero">
<xsl:with-param name="text"
select="substring-after($text,'0')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="removeLeadingSpace">
<xsl:param name="text" />
<xsl:variable name="h1" select="$text" />
<xsl:choose>
<xsl:when test="starts-with($h1,' ')">
<xsl:call-template name="removeLeadingSpace">
<xsl:with-param name="text" select="substring-after($h1,' ')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$h1" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="removeTrailingSpace">
<xsl:param name="text" />
<xsl:variable name="h1" select="$text" />
<xsl:choose>
<xsl:when test="str:ends-with($h1,' ')">
<xsl:call-template name="removeTrailingSpace">
<xsl:with-param name="text" select="str:substringBeforeLast($h1,' ')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$h1" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我生成的输入和输出是:
$ cat newXMLTEST.FILE
<?xml version="1.0" encoding="UTF-8"?><School>
<Student>
<Id_Numer>0000034</Id_Numer>
<Name> David</Name>
<Tot_Marks>000000100</Tot_Marks>
<Last_YearTot_Marks>000000000</Last_YearTot_Marks>
<Fee_Paid>000043.01</Fee_Paid>
</Student>
</School>
$ cat ne.xml
<?xml version="1.0" encoding="UTF-8"?>
<School>
<Student>
<Id_Numer>0000034</Id_Numer>
<Name> David</Name>
<Tot_Marks>000000100</Tot_Marks>
<Last_YearTot_Marks>000000000</Last_YearTot_Marks>
<Fee_Paid>000043.01</Fee_Paid>
</Student>
</School>
但是我正在寻找的东西如下:
<?xml version="1.0" encoding="UTF-8"?>
<School>
<Student>
<Id_Numer>34</Id_Numer>
<Name>David</Name>
<Tot_Marks>100</Tot_Marks>
<Last_YearTot_Marks>0</Last_YearTot_Marks>
<Fee_Paid>43.01</Fee_Paid>
</Student>
</School>
我是XSLT和Xpath的初学者。我修改了一些在线版本的XSLT并尝试使用它。预先感谢。
答案 0 :(得分:0)
如果要删除元素之间的空格,请在样式表元素级别添加以下说明:
<xsl:strip-space elements="*" />
如果要删除text()
节点的前导空格和尾随空格,请添加以下模板:
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
答案 1 :(得分:0)
提供您的输入:
XML
<?xml version="1.0" encoding="UTF-8"?>
<School>
<Student>
<Id_Numer>0000034</Id_Numer>
<Name> David</Name>
<Tot_Marks>000000100</Tot_Marks>
<Last_YearTot_Marks>000000000</Last_YearTot_Marks>
<Fee_Paid>000043.01</Fee_Paid>
</Student>
</School>
以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Name">
<xsl:copy>
<xsl:value-of select="normalize-space(.)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Id_Numer | Tot_Marks | Last_YearTot_Marks | Fee_Paid">
<xsl:copy>
<xsl:value-of select="number(.)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
将产生:
结果
<?xml version="1.0" encoding="UTF-8"?>
<School>
<Student>
<Id_Numer>34</Id_Numer>
<Name>David</Name>
<Tot_Marks>100</Tot_Marks>
<Last_YearTot_Marks>0</Last_YearTot_Marks>
<Fee_Paid>43.01</Fee_Paid>
</Student>
</School>
要使其更通用,请尝试:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="text()[number(.)=number(.)]">
<xsl:value-of select="number(.)"/>
</xsl:template>
</xsl:stylesheet>