XSLT Need Help converting XML to show the value and the attribute

Question

My first post here but here goes,

I have an XML Document that is output from our Scanning system that looks like the following.

<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>  
<DOCUMENTINDEX Name="Company">LR</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Contract Code">L1935</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Entry Date">2016-11-14</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Order Account">04060</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Order Raised By">Jonathan Banks</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Order Total">358</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Purchase Order Date">2016-11-14</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Purchase Order ID">PC022987-1</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Supplier Code">04060</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="Supplier Name">Dynahurst Limited</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="VAT">71.6</DOCUMENTINDEX>
</DOCUMENT>>

I need it to look like the below,

<COMPANY>LR</COMPANY>
<CONTRACT CODE>L1935</CONTRACT CODE> 

ETC ETC ....

I am trying to use XSLT to sort this and so far have managed to pull out the Name part but i can't get the value.

My script so far looks like ...

<?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:apply-templates select="//DOCUMENTINDEX"/>
</xsl:template>

<xsl:template match="DOCUMENTINDEX">
    <br/><xsl:value-of select="@Name"/>
</xsl:template>

Does anyone know how i can do this transformation as a template so i can input this into the scanning software to allow it to transform the document output as they are scanned onto the system.

Which gives me the following output.

Company
Contract Code
Entry Date
Order Account
Order Raised By
Order Total
Purchase Order Date
Purchase Order ID
Supplier Code
Supplier Name
VAT

Thanks

Chris


Show source
| xml   | xslt   | transformation   2016-11-15 12:11 2 Answers

Answers ( 2 )

  1. 2016-11-15 14:11

    Here is one possible solution:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz '" />
        <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'" />
    
        <xsl:template match="/*">
            <xsl:for-each select="DOCUMENTINDEX">
                <xsl:variable name="name" select="@Name" />
                <xsl:element name="{translate($name, $smallcase, $uppercase)}"><xsl:value-of select="text()"/></xsl:element>
            </xsl:for-each>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Please note that you cannot have spaces in the element names. Thus, in the process of transformation, I am replacing them with underscores _

  2. 2016-11-15 14:11

    Helo !

    You can get value of currently processed element with:

    <xsl:value-of select="."/>

    XML Element name is not valid with whitespaces so you can remove them with:

    translate(./@Name,' ','')

    You can create XML element with constructor where you provide its name:

    <xsl:element name="{translate(./@Name,' ','')}"/>

    Final code will look like this:


    <?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:apply-templates select="//DOCUMENTINDEX"/>
        </xsl:template>
        <xsl:template match="DOCUMENTINDEX">
            <xsl:element name="{translate(./@Name,' ','')}">
                <xsl:value-of select="."/>
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    
◀ Go back