Narender Bhadrecha
Narender Bhadrecha

Reputation: 97

XML : Add new field with file URL

I have input file named myfile.xml with below content :

<?xml version="1.0" encoding="UTF-8"?>
<file-format>
<data-set xfer="1.2.840.10008.1.2.1" name="Little Endian Explicit">
<element tag="0008,0018" vr="UI" vm="1" len="64" name="SOPInstanceUID">123</element>
</data-set>
</file-format>

I want to add a field named fileURL and its values should be path of my xml with .xml replaced with .jpg I want an output file with below content :

<?xml version="1.0" encoding="UTF-8"?>
<file-format>
<data-set xfer="1.2.840.10008.1.2.1" name="Little Endian Explicit">
<element tag="0008,0018" vr="UI" vm="1" len="64" name="SOPInstanceUID">123</element>
<element name="fileURL">/user/local/myfile.jpg</element>
</data-set>
</file-format>

Purpose for this is so that I can input this file to solr and use this URL for indexing later. What is best way to do it?

Upvotes: 1

Views: 369

Answers (1)

Amrendra Kumar
Amrendra Kumar

Reputation: 1816

For XSLT this code help you by replace function:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">


    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="element">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
        <xsl:variable name="self-url" select="replace(base-uri(.), '.xml$', '.jpg')"/>
        <element name="{$self-url}"><xsl:value-of select="$self-url"/></element>
    </xsl:template>


</xsl:stylesheet>

Upvotes: 3

Related Questions