Santhosh
Santhosh

Reputation: 55

Transform the xml using

I have an xml like below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="user">1234</entry>
<entry key="name">sam</entry>
</properties>

I want to transform the key value(key="user" to key="cm:user") into a new xml file using xslt, the output xml should be like this

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="cm:user">1234</entry>
<entry key="name">sam</entry>
</properties>

I am using the below xslt and saxon jar:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://schema.infor.com/InforOAGIS/2">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />   
<xsl:template match="@*|node()">
<xsl:result-document href="foo.xml" method="xml">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:result-document>
</xsl:template>

<xsl:template match="@key[.='user']">
<xsl:attribute name="key">
        <xsl:value-of select="'cm:user'"/>
    </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

When I run it I am getting the below error:
XTDE1490: Cannot write more than one result document to the same URI:
Could someone please help me with this..

Upvotes: 1

Views: 81

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

You simply need

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="@key[.='user']">
<xsl:attribute name="key">
        <xsl:value-of select="'cm:user'"/>
    </xsl:attribute>
 </xsl:template>

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

If you want to define the result file name using xsl:result-document then add a template

<xsl:template match="/">
  <xsl:result-document href="foo.xml">
    <xsl:apply-templates/>
  </xsl:result-document>
</xsl:template>

Upvotes: 3

Related Questions