I have an xml where I can get the names of the nodes in the node such as Value, Width, Height, Weight, ... Example:
<?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>
What I want is to dynamically get hte value of the corresponding parent node. I can get the node-names like:
<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>
But I can't figure out how to get the corresponding parent node, and its value like for example the unit element.
I've tried <xsl:value-of select="/Data/$myVariable/@unit" /></td>
, but it doesn't work. I'll keep getting the error: Unexpected token '$myVariable' during parsing of '<AnyKindTest>'
.
You wrote:
<xsl:value-of select="/Data/$myVariable/@unit" />
I think your question is probably a duplicate of
You are trying to use $myVariable
as if the string it holds can be substituted textually into your XPath expression. What you probably want is
/Data/*[name()=$myVariable]/@unit
A lot of people make this mistake, and I'm not sure why. I don't think people would set a variable $op
to "+" or "-" and then expect @x $op @y
to perform addition or subtraction as appropriate. Unless perhaps their previous programming experience was entirely with macro languages.