[英]XSLT apply template on every text node
I have a complex transformation from one xml format to another and need to trim all the possible leading and tailing whitespaces on every text element after the transformation. 我有从一种xml格式到另一种xml格式的复杂转换,并且需要在转换后修剪每个文本元素上所有可能的前导和尾随空格。
XML input is something like: XML输入类似于:
<Records>
<Record>
<FirstName>FIRST</FirstName>
<MiddleName>MID</MiddleName>
<LastName>LAST</LastName>
<VehType>PCAR</VehType>
<VehType2>VN</VehType2>
<VehMake>CHEV</VehMake>
<VehModel>AST</VehModel>
<VehColor>DBL</VehColor>
<VehVIN>1234123</VehVIN>
</Record>
</Records>
And stylesheet example is: 样式表示例为:
<xsl:template match="Record">
<MyRecord>
<NameRecord>
<FirstName>
<xsl:value-of select="FirstName" />
</FirstName>
<MiddleName>
<xsl:value-of select="MiddleName" />
</MiddleName>
<LastName>
<xsl:value-of select="LastName" />
</LastName>
</NameRecord>
<VehicleRecord>
<VehicleType>
<xsl:value-of select="VehType" />
</VehicleType>
<MakeOfCar>
<xsl:value-of select="VehMake" />
</MakeOfCar>
<ModelOfCar>
<xsl:value-of select="VehModel" />
</ModelOfCar>
<ColorPrimary>
<xsl:value-of select="VehColor" />
</ColorPrimary>
<VehicleIDNumber>
<xsl:value-of select="VehVIN" />
</VehicleIDNumber>
<YearOfCar>
<xsl:value-of select="VehYear" />
</YearOfCar>
</VehicleRecord>
</MyRecord>
</xsl:template>
But more complex with a lot of levels and inclusions. 但是更为复杂,其中包含许多级别和包含项。 Problem is, any incoming xml element may contain data like
<VehVIN> 1234123 </VehVIN>
and I need to trim leading and tailing whitespaces. 问题是,任何传入的xml元素都可能包含
<VehVIN> 1234123 </VehVIN>
,我需要修剪前导和尾随空格。 Found this beautiful solution and tried to integrate it in to my stylesheet, but no luck. 找到了这个漂亮的解决方案,并试图将其集成到我的样式表中,但是没有运气。
<xsl:template match="text()">
<xsl:call-template name="string-trim">
<xsl:with-param name="string" select="." />
</xsl:call-template>
</xsl:template>
Instead of doing this... 而不是这样做...
<xsl:value-of select="FirstName" />
Do this, as this will then allow your text() template to match 这样做,因为这将允许您的text()模板匹配
<xsl:apply-templates select="FirstName/text()" />
And similarly for the other elements. 其他元素也是如此。
Infact, you can just to <xsl:apply-templates select="FirstName" />
as XSLT's built-in template will match FirstName
which will then skip over the node and select its child nodes. 实际上,您只能对
<xsl:apply-templates select="FirstName" />
因为XSLT的内置模板将与FirstName
匹配, FirstName
会跳过该节点并选择其子节点。
You can use the following template: 您可以使用以下模板:
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
It removes all leading and trailing spaces on every text element. 它将删除每个文本元素上的所有前导和尾随空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.