【发布时间】:2019-05-22 17:03:47
【问题描述】:
请帮助:尝试通过删除前导零和空格来转换 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 的在线版本并尝试使用它。提前致谢。
【问题讨论】:
-
你能澄清你所说的“它不工作”是什么意思吗?如果正确调用,模板将删除前导空格(尽管例如,如果有换行符作为第一个字符,则不会)。显示一些示例输入和您当前获得的输出(以及您所期望的)可能会有所帮助。谢谢!