这是我的XML-
<?xml version="1.0" encoding="UTF-8"?>
<update-this>
<document name="http://blah">
<amount>015-39</amount>
<shipping>US::Ground:0.00</shipping>
<c:karoke:string>abcabc</c:karoke:string>
<size>11.5 D</size>
<price>100.00 USD</price>
<stock>present in stock </stock>
</document>
</update-this>
这就是我想要的最终XML-
<?xml version="1.0" encoding="UTF-8"?>
<document name="http://blah">
<amount>015-39</amount>
<shipping>US::Ground:0.00</shipping>
<karoke>abcabc</karoke>
<size>11.5 D</size>
<price>100.00 USD</price>
<stock>present in stock </stock>
<size_stock>11.5D_presentinstock</size_stock>
<size_present>11.5D_present>
<discounted_price>90 USD</discounted_price>
<increased_price>110 USD</increased_price>
</document>
这是XSLT-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="document">
<xsl:variable name="price" select="substring-before(normalize-space(price),' ')"/>
<xsl:variable name="currency" select="substring-after(normalize-space(price),' ')"/>
<xsl:variable name="difference" select="number($price) * .10"/>
<xsl:variable name="size">
<xsl:value-of select="size"/>
</xsl:variable>
<xsl:variable name="stock">
<xsl:value-of select="stock"/>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<discounted_price><xsl:value-of select="concat(format-number($price - $difference,'#,###.00'),' ',$currency)"/></discounted_price>
<increased_price><xsl:value-of select="concat(format-number($price + $difference,'#,###.00'),' ',$currency)"/></increased_price>
<size_stock> <xsl:value-of select="concat($size,'_',$stock)" /></size_stock>
<size_present><xsl:value-of select="concat($size,'_','present')" /></size_present>
</xsl:copy>
<xsl:choose>
<xsl:when test="starts-with(name(), 'c:karoke:string')">
<xsl:element name="karoke">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="size_stock | size_present">
<xsl:copy>
<xsl:value-of select="translate(., ' ', '')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我使用XSLT 1.0,我的XML有多个文档元素。为什么元素c:karoke:string重命名为karoke不工作?同样,size_stock和size_present中的空格不会被删除。你能帮帮我吗?
当你这样做的时候:
<xsl:when test="starts-with(name(), 'c:karoke:string')">
你在上下文中:
<xsl:template match="document">
因此,name()
函数将返回不以'c:karoke:string'
开头的字符串"document"
,因此该测试将永远不会返回true。