Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt - selecting an attribute value from an xml file

Tags:

xml

xslt

I have a XML file as:

<BatchTable>
  <UUThref SocketIndex='0 - CCM'
           UUTResult='Passed'
           URL='C:\OverrideCallbacks_BatchReport[4 16 2012][4 14 18 PM].xml'
           FileName='OverrideCallbacks_BatchReport[4 16 2012][4 14 18 PM].xml'
           ECAFailCount='1'
           Version='StationPartNumber=55555StationSerialNumber=2222TPSPartNumber=1234'/>
</BatchTable>

In order to pick the Version in the xsl file I have:

<xsl:value-of select="BatchTable/UUThref/[@Version]"/>

This does not return any value. What am I doing wrong?

like image 538
rkk Avatar asked Apr 17 '12 00:04

rkk


People also ask

How do you get the value of a variable in XSLT?

The XSLT <xsl:value-of> element is used to extract a value from an expression defined in the select attribute. The expression defined in the mandatory select attribute is either an XPATH expression (for nodes and/or values) or a variable reference. The return of the <xsl:value-of> function is a literal value.

How do you display XML elements in an HTML table using XSLT?

Create an XSL stylesheet.xml version=”1.0″?> To bind the XML elements to a HTML table, the <for-each> XSL template must appear before each table row tag. This ensures that a new row is created for each <book> element. The <value-of> template will output the selected text of each child element into a separate table.


1 Answers

It should be

BatchTable/UUThref/@Version

not

BatchTable/UUThref/[@Version]

...where are you getting the square brackets from?


I've tested the following, and it definitely works:

xmlstarlet sel -t -m 'BatchTable/UUThref/@Version' -v . <test.xml

...this command line works by applying the following XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="BatchTable/UUThref/@Version">
      <xsl:call-template name="value-of-template">
        <xsl:with-param name="select" select="."/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="value-of-template">
    <xsl:param name="select"/>
    <xsl:value-of select="$select"/>
    <xsl:for-each select="exslt:node-set($select)[position()&gt;1]">
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
like image 176
Charles Duffy Avatar answered Oct 01 '22 04:10

Charles Duffy