我正在处理这个XML:
<Brand>
<Brand_Name>BLENDERM</Brand_Name>
<Brand_Code>1103</Brand_Code>
<Groups>
<Group>
<Group_Code>657</Group_Code>
<Parent_Code>0</Parent_Code>
<Group_Level>1</Group_Level>
<Group_Name>Brand Default</Group_Name>
<Product>
<Pip_code>0032359</Pip_code>
<Status>In Use</Status>使用此XSLT:
<xsl:template match="Product" mode="phase-3">
<xsl:value-of select="document('rx_catmapping.xml')/descendant::mapping[source=substring(ancestor::Brand/Brand_Name,1,1)]/target"/>
</xsl:template>以下是rx_catmapping.xml的示例:
<Lookup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<mapping>
<source>a</source>
<target>788</target>
</mapping>
<mapping>
<source>B</source>
<target>789</target>
</mapping>
</Lookup>因此,我正在处理Product元素,它是Brand的后代。在本例中,Brand/Brand_Name的第一个字母是B,我试图通过在rx_catmapping.xml中查找来输出值789。这应该很简单,但我完全被难住了!我尝试将XPath的第一部分更改为document('rx_catmapping.xml')/Lookup/mapping或document('rx_catmapping.xml')//mapping。我还尝试将比较的前半部分更改为string(source)或source/text(),但这两种方法都不起作用。(尝试这样做的原因是,例如,使用source='B'似乎确实有效,所以我想知道我是否正在尝试比较两种不兼容的数据类型。)
提前感谢您的帮助。
发布于 2011-08-12 01:49:36
定义一个键
<xsl:key name="k1" match="mapping" use="source"/>然后使用
<xsl:variable name="map-doc" select="document('rx_catmapping.xml')"/>和
<xsl:variable name="letter" select="substring(ancestor::Brand/Brand_Name,1,1)"/>
<xsl:for-each select="$map-doc">
<xsl:value-of select="key('k1', $letter)/target"/>
</xsl:for-each>使用XSLT2.0,您可以将其简化为
<xsl:value-of select="key('k1', substring(ancestor::Brand/Brand_Name,1,1), $map-doc)"/>发布于 2011-08-12 01:57:02
我认为问题在于,在执行ancestor::Brand/Brand_Name时,上下文是外部文件中的mapping元素。这对我很有效
<xsl:template match="/">
<xsl:apply-templates select=".//Product"/>
</xsl:template>
<xsl:template match="Product">
<xsl:variable name="x" select="substring(ancestor::Brand/Brand_Name,1,1)"/>
<xsl:value-of select="document('file:///c:/temp/rx_catmapping.xml')//mapping[source=$x]/target"/>
</xsl:template>https://stackoverflow.com/questions/7030383
复制相似问题