Tuesday, 11 October 2016

XML : XSL Check current node with variable value

I want to check if a variable (array) is equal to the current element attribute.

The <Test> tags are there just to check if the forEach Loop is working.

transform.xsl

  <?xml version="1.0" encoding="utf-8"?>  <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/">        <xsl:variable name="item" select="document('../XmlFiles/Mapping.xml')//Mapping" />        <xsl:for-each select="$item[@Key = current()/@key]">        <Test></Test>      </xsl:for-each>      </xsl:template>    </xsl:stylesheet>    

Mapping.xml

  <?xml version="1.0" encoding="utf-8" ?>  <Mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    <Mapping ID="A" Key="Key1" />    <Mapping ID="B" Key="Key2" />    <Mapping ID="C" Key="Key3" />  </Mappings>    

Source.xml

  <?xml version="1.0" encoding="utf-8" ?>  <item key="Key1">  </item>    

The Xml Result Output I try to make:

   <?xml version="1.0" encoding="utf-8" ?>   <item key="Key1" mapping="A">   </item>    

No comments:

Post a Comment