动态获取父节点及其值

问题描述 投票:0回答:2

我有一个 xml,可以在其中获取节点中节点的名称,例如值、宽度、高度、重量…… 示例:

<?xml version="1.0" encoding="utf-8"?>
<Data totalExecutionTime="00:00:00.0076034" totalCount="1">
  <Features>
    <ProductID 
               id="100" 
               language="NLB" 
               caption="Product ID" 
               captionAlternative="Product ID" 
               unit="" 
               help="" 
               abbr="" 
               groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
               group="Alles" 
               groupOrder="0" 
               viewGroupId="" 
               viewGroup="" 
               viewGroupOrder="0" 
               topViewGroupId="b65b432a-1bf1-4fd4-ae92-03418d3204be" 
               topViewGroup="Algemeen" 
               topViewGroupOrder="1" 
               viewOrder="0" 
               form="Complex" 
               dataType="String" 
               readOnly="False" />
    <Length 
            id="245" 
            language="NLB" 
            caption="Lengte" 
            captionAlternative="Lengte" 
            unit="mm" 
            help="" 
            abbr="" 
            groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
            group="Alles" 
            groupOrder="0" 
            viewGroupId="28813941-54b4-4511-b91e-d9ae53aa03e9" 
            viewGroup="Afmetingen en gewicht" 
            viewGroupOrder="91" 
            topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
            topViewGroup="Specificaties" 
            topViewGroupOrder="4" 
            viewOrder="1" 
            form="Simple" 
            dataType="Number" 
            readOnly="False" />
    <Width 
           id="242" 
           language="NLB" 
           caption="Breedte" 
           captionAlternative="Breedte" 
           unit="mm" 
           help="" 
           abbr="" 
           groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
           group="Alles" 
           groupOrder="0" 
           viewGroupId="28813941-54b4-4511-b91e-d9ae53aa03e9" 
           viewGroup="Afmetingen en gewicht" 
           viewGroupOrder="91" 
           topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
           topViewGroup="Specificaties" 
           topViewGroupOrder="4" 
           viewOrder="2" 
           form="Simple" 
           dataType="Number" 
           readOnly="False" />
    <Height 
            id="244" 
            language="NLB" 
            caption="Hoogte" 
            captionAlternative="Hoogte" 
            unit="mm" 
            help="" 
            abbr="" 
            groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
            group="Alles" 
            groupOrder="0" 
            viewGroupId="28813941-54b4-4511-b91e-d9ae53aa03e9" 
            viewGroup="Afmetingen en gewicht" 
            viewGroupOrder="91" 
            topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
            topViewGroup="Specificaties" 
            topViewGroupOrder="4" 
            viewOrder="3" 
            form="Simple" 
            dataType="Number" 
            readOnly="False" />
    <Depth 
           id="771" 
           language="NLB" 
           caption="Diepte" 
           captionAlternative="Diepte" 
           unit="mm" 
           help="" 
           abbr="" 
           groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
           group="Alles" 
           groupOrder="0" 
           viewGroupId="28813941-54b4-4511-b91e-d9ae53aa03e9" 
           viewGroup="Afmetingen en gewicht" 
           viewGroupOrder="91" 
           topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
           topViewGroup="Specificaties" 
           topViewGroupOrder="4" 
           viewOrder="4" 
           form="Simple" 
           dataType="Number" 
           readOnly="False" />
    <Weight 
            id="243" 
            language="NLB" 
            caption="Gewicht" 
            captionAlternative="Gewicht"
            unit="kg" 
            help="" 
            abbr="" 
            groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
            group="Alles" 
            groupOrder="0" 
            viewGroupId="28813941-54b4-4511-b91e-d9ae53aa03e9" 
            viewGroup="Afmetingen en gewicht" 
            viewGroupOrder="91" 
            topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
            topViewGroup="Specificaties" 
            topViewGroupOrder="4" 
            viewOrder="5" 
            form="Simple" 
            dataType="Number" 
            readOnly="False" />
    <LoadCapacity 
                  id="457" 
                  language="NLB" 
                  caption="Draagvermogen" 
                  captionAlternative="Draagvermogen"
                  unit="kg" 
                  help="" 
                  abbr="" 
                  groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
                  group="Alles" 
                  groupOrder="0" 
                  viewGroupId="c9d7dd7e-8a85-47cc-8542-2695e4615f75" 
                  viewGroup="Technische specificaties"
                  viewGroupOrder="15" 
                  topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
                  topViewGroup="Specificaties" 
                  topViewGroupOrder="4" 
                  viewOrder="6" 
                  form="Simple" 
                  dataType="Number" 
                  readOnly="False" />
    <Volume 
            id="435" 
            language="NLB" 
            caption="Inhoud" 
            captionAlternative="Inhoud" 
            unit="l" 
            help="" 
            abbr="" 
            groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
            group="Alles" 
            groupOrder="0" 
            viewGroupId="bfaf678b-8b9d-4cea-aa99-08edd8316dc7" 
            viewGroup="Algemene specificaties" 
            viewGroupOrder="13" 
            topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82"
            topViewGroup="Specificaties" 
            topViewGroupOrder="4" 
            viewOrder="7" form="Simple" 
            dataType="Number" 
            readOnly="False" />
    <TireSize 
              id="705" 
              language="NLB" 
              caption="Banden maat" 
              captionAlternative="Banden maat" 
              unit="mm" 
              help="" 
              abbr="" 
              groupId="73660834-eea8-4d67-8e84-b38e2742706a" 
              group="Alles" 
              groupOrder="0" 
              viewGroupId="c9d7dd7e-8a85-47cc-8542-2695e4615f75" 
              viewGroup="Technische specificaties" 
              viewGroupOrder="15" 
              topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
              topViewGroup="Specificaties" 
              topViewGroupOrder="4" 
              viewOrder="8" 
              form="Simple" 
              dataType="String" 
              readOnly="False" />
    <LoadingScoop
                  id="706" 
                  language="NLB" 
                  caption="Laadschep" 
                  captionAlternative="Laadschep" 
                  unit="mm" 
                  help="" 
                  abbr="" 
                  groupId="73660834-eea8-4d67-8e84-b38e2742706a"
                  group="Alles" 
                  groupOrder="0" 
                  viewGroupId="c9d7dd7e-8a85-47cc-8542-2695e4615f75" 
                  viewGroup="Technische specificaties" 
                  viewGroupOrder="15" 
                  topViewGroupId="590c0ee7-e1eb-4e18-b583-eba8984ffe82" 
                  topViewGroup="Specificaties" 
                  topViewGroupOrder="4" 
                  viewOrder="9" 
                  form="Simple" 
                  dataType="String" 
                  readOnly="False" />
  </Features>
  <ProductID id="25137" parentId="0" brand="Normal" order="0" createdDate="2020-03-24T15:30:07.21" modifiedDate="2023-11-20T20:01:33.87">
    <Value seq="0" modifiedDate="2020-03-24T15:30:10.493">111TA5851</Value>
    <Width seq="0" modifiedDate="2020-03-24T15:30:10.493">515</Width>
    <Height seq="0" modifiedDate="2020-03-24T15:30:10.493">1100</Height>
    <Weight seq="0" modifiedDate="2020-04-02T13:18:09.44">7.7</Weight>
    <LoadCapacity seq="0" modifiedDate="2020-03-24T15:30:10.493">150</LoadCapacity>
    <LoadingScoop seq="0" modifiedDate="2020-03-24T15:30:10.493">350x170</LoadingScoop>
  </ProductID>
</Data>

我想要的是动态获取对应父节点的hte值。我可以获得如下节点名称:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <table>
        <xsl:for-each select="/Data/ProductID/*">
            <xsl:variable name="myVariable" select="name(.)"/>
            <xsl:variable name="HasValue" select="count(/Data/ProductID/*)" />
            <tr>
                <td><xsl:value-of select="name(.)" /></td>
                <td><xsl:value-of select="local-name()" /></td>
                <td><xsl:value-of select="name()" /></td>
                <td><xsl:value-of select="/Data/node($myVariable)/@unit" /></td>
            </tr>
        </xsl:for-each>
    </table>
    </xsl:template>
</xsl:stylesheet>

但是我不知道如何获取相应的父节点及其值,例如单位元素。

我已经尝试过

<xsl:value-of select="/Data/$myVariable/@unit" /></td>
,但不起作用。我会不断收到错误:
Unexpected token '$myVariable' during parsing of '<AnyKindTest>'

xml xslt
2个回答
0
投票

我想你想声明一把钥匙

<xsl:key name="element-key" match="Data/Features" use="name()"/>

然后你想使用例如

key('element-key', name())/@unit


0
投票

你写道:

<xsl:value-of select="/Data/$myVariable/@unit" />

我认为你的问题可能是重复的

使用 2 个参数进行 xslt 测试

您正在尝试使用

$myVariable
,就好像它所保存的字符串可以以文本方式替换到您的 XPath 表达式中一样。您可能想要的是

/Data/*[name()=$myVariable]/@unit

很多人都犯过这个错误,但我不知道为什么。我不认为人们会将变量

$op
设置为“+”或“-”,然后期望
@x $op @y
执行适当的加法或减法。除非他们以前的编程经验完全是用宏语言。

© www.soinside.com 2019 - 2024. All rights reserved.