0
votes

First of all, my apologies for the convoluted Title, but I hope that the example information I am posting will make my question clearer!

I have an XML document that looks like the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Setting Name="LDAPS" Type="htf:map">

<Setting Name="LDAP-A" Type="htf:map">
  <Setting Name="MY_URL" Type="xsd:string">ldap://xxxx:yyy</Setting>
  <Setting Name="Name" Type="xsd:string">MY_LDAP1</Setting>
  <Setting Name="somethingelse" Type="xsd:string">SOMEOTHERVALUE1</Setting>
</Setting>


<Setting Name="LDAP-B" Type="htf:map">
  <Setting Name="MY_URL" Type="xsd:string">ldap:abc:xyz</Setting>
  <Setting Name="Name" Type="xsd:string">MY_TARGET_LDAP</Setting>
  <Setting Name="somethingelse" Type="xsd:string">SOMEOTHERVALUE2</Setting>
  <Setting Name="somethingnew" Type="xsd:string">SOMENEWVALUE</Setting>
</Setting>


<Setting Name="LDAP-C" Type="htf:map">
  <Setting Name="MY_URL" Type="xsd:string">ldap:abc:xxxx</Setting>
  <Setting Name="Name" Type="xsd:string">MY_LDAP3</Setting>
  <Setting Name="somethingelse" Type="xsd:string">SOMEOTHERVALUE3</Setting>
  <Setting Name="somethingelseotherdata" Type="xsd:string">SOMEOTHERDATAVALUE3</Setting>
</Setting>

</Setting>

</Configuration>

Given the XML above, I am FIRST trying to find the IF there is a node that has the Xpath /Configuration/Setting/Setting that has a sub-node that has an attribute named "Name" and where the value of the sub-node matches a value I am looking for (e.g. In the example data above, the first thing I am trying to find is the sub-node in the XML whose "Name" attribute is "Name", and where the value of the node is "MY_TARGET_LDAP").

Then, IF there is such a sub-node, what I trying to find (and this is the end goal) is the value of "Name" attribute of /Configuration/Setting/Setting node.

So, for the example XML above, what the final result should be:

LDAP-B

I am trying to do most of the work using XSLT, and so far, what I have is the following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>

<xsl:template match="/">
  <html>
  <body>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="/Configuration/Setting">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="/Configuration/Setting/Setting">
  <ldapname> <xsl:value-of select="./@Name"/> </ldapname>
  <xsl:for-each select="*">
      <response>
          <respname> <xsl:value-of select="./@Name"/> </respname>
              <respvalue><xsl:value-of select="."/> </respvalue>
          </response> <xsl:text>&#xa;</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

I am using xsltproc on a Redhat machine to test and when I run that against the sample XML I am getting:

xsltproc generic1.xsl generic-test.xml:

<html xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">    <body>

<ldapname>LDAP-A</ldapname><response><respname>MY_URL</respname><respvalue>ldap://xxxx:yyy</respvalue></response>
<response><respname>Name</respname><respvalue>MY_LDAP1</respvalue></response>
<response><respname>somethingelse</respname><respvalue>SOMEOTHERVALUE1</respvalue></response>

<ldapname>LDAP-B</ldapname><response><respname>MY_URL</respname><respvalue>ldap:abc:xyz</respvalue></response>
<response><respname>Name</respname><respvalue>MY_TARGET_LDAP</respvalue></response>
<response><respname>somethingelse</respname><respvalue>SOMEOTHERVALUE2</respvalue></response>
<response><respname>somethingnew</respname><respvalue>SOMENEWVALUE</respvalue></response>

<ldapname>LDAP-C</ldapname><response><respname>MY_URL</respname><respvalue>ldap:abc:xxxx</respvalue></response>
<response><respname>Name</respname><respvalue>MY_LDAP3</respvalue></response>
<response><respname>somethingelse</respname><respvalue>SOMEOTHERVALUE3</respvalue></response>
<response><respname>somethingelseotherdata</respname><respvalue>SOMEOTHERDATAVALUE3</respvalue>              </response>

</body></html>

(NOTE: Please don't pay attention to the BODY and HTML tags in the above... I am not actually using HTML but it just makes it easier for me to look at when I test.)

In any event, can someone suggest how I can modify the XSLT to accomplish what I described above?

Thanks! Jim

1

1 Answers

1
votes

If I follow this correctly, all you need to do is:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:value-of select="Configuration/Setting/Setting[Setting[@Name='Name']='MY_TARGET_LDAP']/@Name"/>
</xsl:template>

</xsl:stylesheet>

Note that this assumes there is at most one Setting that satisfies the criteria in predicates. If there are more, only the value of the Name attribute of the first one of these will be returned.