【发布时间】:2015-10-28 12:39:02
【问题描述】:
我有一个相当复杂的问题 (IMO),所以我希望我的意思很清楚。
考虑具有以下结构的 XML。
<products>
<product exportName="XPOZER_90X120">
<product-options>
<option productOptionRef="xpozerFrame">
<plan>
<period startDate="2015-10-01">
<price name="true" netBasePrice="34"/>
<price name="false" netBasePrice="0"/>
</period>
</plan>
</option>
</product-options>
</product>
</products>
该产品有两个“价格”子节点。 对于存在的每个“价格”子节点,都需要将其转换为一个“产品”节点,同时必须将 exportName 属性更改为生成的值。其余节点保持不变。
想要的输出:
<products>
<product exportName="XPOZER_WITHFRAME90X120">
<product-options>
<option productOptionRef="xpozerFrame">
<plan>
<period startDate="2015-10-01">
<price name="true" netBasePrice="34"/>
<price name="false" netBasePrice="0"/>
</period>
</plan>
</option>
</product-options>
</product>
<product exportName="XPOZER_NOFRAME90X120">
<product-options>
<option productOptionRef="xpozerFrame">
<plan>
<period startDate="2015-10-01">
<price name="true" netBasePrice="34"/>
<price name="false" netBasePrice="0"/>
</period>
</plan>
</option>
</product-options>
</product>
</products>
exportname 变成 'XPOZER_NOFRAME90X120' 和 'XPOZER_WITHFRAME90X120' 用同一个 xslt 中的文档函数计算,然后做一些转换。
我已经完成了产品节点转换,但我一直在更改 exportName 属性。它必须成为的值已经起作用,但我不知道如何在复制时实际更改 exportName 属性。
这是迄今为止的 xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
xmlns:input="urn:input-variables"
xmlns:my="my:my">
<my:values>
<xpozerFramefalse>NOFRAME</xpozerFramefalse>
<xpozerFrametrue>WITHFRAME</xpozerFrametrue>
<XPOZER_90X120>XPOZER_[xpozerFrame]90X120</XPOZER_90X120>
</my:values>
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="products/product[@exportName='XPOZER_90X120']" />
</xsl:template>
<xsl:template match="product[product-options/option/plan/period/price]">
<xsl:for-each select="product-options/option/plan/period/price">
<xsl:variable name="vNewExportName">
<xsl:call-template name="priceNode" />
</xsl:variable>
<xsl:copy-of select="../../../../.." />
</xsl:for-each>
</xsl:template>
<xsl:template name="priceNode">
<xsl:variable name="vProductOptionRef" select="../../../@productOptionRef" />
<xsl:variable name="vSelector" select="concat($vProductOptionRef, @name)" />
<xsl:variable name="vExportname" select="../../../../../@exportName" />
<xsl:variable name="vNewExportName">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="document('')//my:values/*[name()=$vExportname]" />
<xsl:with-param name="replace" select="concat(concat('[', $vProductOptionRef), ']')" />
<xsl:with-param name="by" select="document('')//my:values/*[name()=$vSelector]" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$vNewExportName" />
</xsl:template>
</xsl:stylesheet>
因此,带有 copy-of 的 for-each 负责将节点两次复制到转换后的 XML 中,并且通过从“my:values”中检索值来正确找到 exportname 应该更改为的值。 但我真的不知道如何将值应用于实际属性。
谁能帮我解决这个问题?这甚至可以在不依赖编码的情况下实现吗?
【问题讨论】:
-
您能 1) 解释如何计算 exportName 2) 并为我们提供一个示例预期输出吗?谢谢。