首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT通过比较元素文本和子字符串结果来查找匹配节点

XSLT通过比较元素文本和子字符串结果来查找匹配节点
EN

Stack Overflow用户
提问于 2011-08-12 01:38:25
回答 2查看 394关注 0票数 1

我正在处理这个XML:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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的示例:

代码语言:javascript
复制
<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/mappingdocument('rx_catmapping.xml')//mapping。我还尝试将比较的前半部分更改为string(source)source/text(),但这两种方法都不起作用。(尝试这样做的原因是,例如,使用source='B'似乎确实有效,所以我想知道我是否正在尝试比较两种不兼容的数据类型。)

提前感谢您的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-12 01:49:36

定义一个键

代码语言:javascript
复制
<xsl:key name="k1" match="mapping" use="source"/>

然后使用

代码语言:javascript
复制
<xsl:variable name="map-doc" select="document('rx_catmapping.xml')"/>

代码语言:javascript
复制
<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,您可以将其简化为

代码语言:javascript
复制
<xsl:value-of select="key('k1', substring(ancestor::Brand/Brand_Name,1,1), $map-doc)"/>
票数 1
EN

Stack Overflow用户

发布于 2011-08-12 01:57:02

我认为问题在于,在执行ancestor::Brand/Brand_Name时,上下文是外部文件中的mapping元素。这对我很有效

代码语言:javascript
复制
  <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>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7030383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档