XSLT How to access attribute from different tag level?

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


XSLT How to access attribute from different tag level?



I am new to xsl & xslt. I am using xslt for xml to xml conversion. I have below xml file. I am iterating through REQ-IF/record using for-each loop and inside the loop I want to iterate REQ-IF/Attributes/Attribute to access @Name value. I tried using ../ but it only gives one level up values and do not allow me to iterate. Could you please help me with this? Thanks.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<REQ-IF>
<record>
<Name>ABC</Name>
<Presentationname>Task Record</Presentationname>
<GUID>123</GUID>
</record>
<record>
<Name>DEF</Name>
<Presentationname>Role Record</Presentationname>
<GUID>456</GUID>
</record>
<record>
<Name>GHI</Name>
<Presentationname>WorkProduct Record</Presentationname>
<GUID>789</GUID>
</record>
<Attributes>
<Attribute>
<Name>Task</Name>
</Attribute>
<Attribute>
<Name>Role</Name>
</Attribute>
<Attribute>
<Name>WorkProduct</Name>
</Attribute>
</Attributes>
</REQ-IF>





Can you post what you have tried so far, and explain what result did you expect and what you get?
– Little Santi
20 mins ago




1 Answer
1



That works


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:template match="/REQ-IF">
<xsl:for-each select="record">
<xsl:text> </xsl:text>
<xsl:value-of select="./Name"/>
<xsl:for-each select="../Attributes/Attribute">
<xsl:text> </xsl:text>
<xsl:value-of select="Name"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>



output:


ABC
Task
Role
WorkProduct
DEF
Task
Role
WorkProduct
GHI
Task
Role
WorkProduct






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

2CH1rxV1eKdbqzYe3CVMo2t,W31jU1UWwtTAYXBfEi0WXM Wn8hySb4iJgK2a puH,ggcj 4DSH2A7dy8lcpmb3CZH,xvgQ6 AcPjH
e6udXi2Ijm0XOvOVuee,FagMr,7efciusf0fca3x8fL3 BVV,cC,LyxeLXLIIvb3tGrCo4shASj8

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham