【问题标题】:XSL - how to next for each loop inside another for each to link the same values from different xml nodesXSL - 如何在另一个循环中为每个循环下一个链接来自不同 xml 节点的相同值
【发布时间】:2019-10-03 20:53:31
【问题描述】:

我正在努力使用 xslt 创建一个 Excel,它将通过公共属性值链接(连接)同一级别上的两个节点。

这里是输入xml:

```
<Nodes>
<Item name="ABC" category_id="A"></Item>
<Item name="DEF" category_id="B"></Item>
<Category name="First category" cat_id="A"</Category>
<Category name="Second category" cat_id="B"</Category>
</Nodes>```

和 xsl 的一部分:

        <xsl:for-each select="//tc:Nodes/tc:Item">
             <xsl:variable name="item_name" select="./@Name" />
             <xsl:variable name="item_category_id" select="./@category_id" />
             <xsl:for-each select="//tc:Nodes:tc:Category/@cat_id = $category_id">
               <xsl:variable name="category_category_id" select="./@cat_id />
             </xsl:for-each>
             <xsl:call-template name="generateReportData">
               <xsl:with-param name="item_name" select="$item_name"/>
               <xsl:with-param name="item_category_id" select="$item_category_id" />
               **<xsl:with-param name="category_category_id" select="$category_category_id"/>**
             </xsl:call-template>

         </xsl:for-each>

问题是我无法访问变量 $category_category_id 因为它说它要么未定义要么超出范围。

结果应该有一行包含以下值:ABC、A、第一个类别(它将通过公共类别 ID 链接来自两个节点的值)。请帮助 - 我是 xsl 的新手 - 也许还有另一种方法。

问候,卢克

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    通过共同的属性值链接(连接)同一级别的两个节点。

    最好使用 key 来完成。例如:

    XML

    <Nodes>
        <Item name="ABC" category_id="A"/>
        <Item name="DEF" category_id="B"/>
        <Category name="First category" cat_id="A"/>
        <Category name="Second category" cat_id="B"/>
    </Nodes>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:key name="cat" match="Category" use="@cat_id" />
    
    <xsl:template match="Nodes">
        <Table>
            <xsl:for-each select="Item">
                <Row>
                    <Cell>
                        <xsl:value-of select="@name"/>
                    </Cell>
                    <Cell>
                        <xsl:value-of select="key('cat', @category_id)/@name"/>
                    </Cell>
                </Row>
            </xsl:for-each>
        </Table>
    </xsl:template>
    
    </xsl:stylesheet>
    

    结果

    <?xml version="1.0" encoding="UTF-8"?>
    <Table>
      <Row>
        <Cell>ABC</Cell>
        <Cell>First category</Cell>
      </Row>
      <Row>
        <Cell>DEF</Cell>
        <Cell>Second category</Cell>
      </Row>
    </Table>
    

    【讨论】:

    • 嗨,Michael,谢谢,这个 xsl:key 元素是否可能不受支持?我收到这样的消息,它在命名空间 xmlns:xsl="w3.org/1999/XSL/Transform" 中不受支持。
    • 您使用的是哪种 XSLT 处理器?
    • 我正在使用 Microsoft Visual Studio,如果您能提出更好的建议,请告诉 :)
    • 不,我不相信这是可能的。
    • @Lukasz - 你使用什么版本的 Visual Studio?使用xsl:key 应该没有问题。严格来说,Visual Studio 本身并不是 XSLT 处理器,但(可能)在幕后使用 MSXML.dll。也许它使用的是一个非常旧的版本。 (见support.microsoft.com/en-gb/help/278674/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 2016-11-21
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多