Stamatia Ch
Stamatia Ch

Reputation: 104

xsl to xml transformation adding non existing field?

I have some thousands of xsl files and I need to transformer them in order to index them in Solr. I have done that but I need every file to have a field ID same as the file's name. The files look like this:

<TEXT>&#2;
<TITLE>A Title</TITLE>
<DATELINE>    A Dateline </DATELINE>
<BODY> text </BODY>
</TEXT>

For example: my files' names are like "0001.xsl", "0002.xsl"..etc. I need to have something like:

<?xml version="1.0" encoding="UTF-8"?>
<add>
    <doc>
        <field name="ID">0001</field>
        <field name="TITLE">-a tile-</field>
        <field name="DATELINE">-a dateline-</field>
        <field name="BODY"> -text-</field>
</field>
    </doc>
</add>

This is the stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  

  <!-- use this for debugging only -->
  <!-- <xsl:output method="xml" indent="yes"/> -->

  <xsl:template match="*"> 
    <xsl:element name="field">
      <xsl:attribute name="name">
        <xsl:value-of select="name()"/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="/">
    <add>
      <doc>        
        <xsl:apply-templates select="//TEXT/*"/>
      </doc>
    </add>
  </xsl:template>

</xsl:stylesheet>

How exactly can I do that? Do I need to change the stylesheeet or can I maybe use Java to add the specific field after I read the file's name? I am using Java for transforming the files and indexing them.

Upvotes: 0

Views: 114

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167461

As you are using Java you could switch to Saxon and XSLT 2.0 where you have functions to read out the document URI and tokenize to split it up to extract the file name. Or with an XSLT 1.0 processor, assuming you run the transformation with your Java code that knows the file name anyway to run the transformation on it you could consider to define a global parameter that your Java code sets to the file name before running the transformation and that the XSLT reads out:

  <xsl:param name="file-id"/>

  <xsl:template match="/">
    <add>
      <doc> 
        <field name="ID"><xsl:value-of select="$file-id"/></field>       
        <xsl:apply-templates select="//TEXT/*"/>
      </doc>
    </add>
  </xsl:template>

Note that your other template is rather verbose, it could be shortened to

  <xsl:template match="*"> 
    <field name="{name()}">
      <xsl:value-of select="."/>
    </field>
  </xsl:template>

Upvotes: 1

Related Questions