Question

I have a bunch of XML that all conform to the same schema. A particular element I want to batch edit only occurs exactly once each of the XML files and has an identical xPath in each of these files.

e.g.  /Valid/HeaderInfo/SoftwareID

I want to create a script/procedure so that I can replace the value (I believe it is more accurately called the text value of the node) for this specific element and perform that update to all my XML files in a folder of group of folders. For instance right now it is:

<SoftwareID>12451245</SoftwareID>

I want it to be

<SoftwareID>53623745</SoftwareID>

instead.

I am currently just starting out in the world of programming, as well as learning about XML data in general - and I need some fundamental information about how to even start. What would be the best way to do this? I have Altova XMLSpy and I know there is a scripting component to it. But is it more appropriate to do this in a specific programming language (I am currently learning Visual Basic) or is there some other software that exisits for performing these types of batch updates?

Any information that would be put me in the right direction would be great!

Thanks!

Update (06/26/13)

The XPath to FilingSoftwareId (and the updated element name) is actually:

  ValidFiling/FilingHeader/FilingSoftwareId  

With ValidFiling being the root of the XML document. I used what you provided and updated accordingly but my result is a duplicate of my original XML file when I select this XSL file for XSL Transformation in Altova XMLSpy.

Is it possible that the update to the FilingSoftwareID is being replaced with the original value when the second catch-all template is applied to the document?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
    <xsl:template match="/ValidFiling/FilingHeader/FilingSoftwareId">
        <FilingSoftwareId>
            <xsl:text>243523452345</xsl:text>
        </FilingSoftwareId>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Thanks again!

Was it helpful?

Solution

I would write an identity-translate XSL and then apply that XSL to all the XMLs in that folder using whatever batch technology you wish (.bat, VB app if you like). Write a match for the specific element or elements you wish to change and then include a general template that outputs all the content of everything else as is.

Without testing, it should be something like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="//Valid/HeaderInfo/SoftwareID">
    <SoftwareID>
        <xsl:text>53623745</xsl:text>
    </SoftwareID>        
</xsl:template>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

You could even expand that example to pass in the new value as a parameter into the transform so you never have to even edit it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top