Re: [xml] xml find and replace

Message: 1
Date: Mon, 16 Jul 2012 11:09:56 +0100
From: stuart shepherd <jonny wark googlemail com>

<xsl:template match="//control[name='DelaySeconds']/numeric/max">
<xsl:element name="{name(.)}">
        <xsl:value-of select="translate(string(.), '110', '5')" />

translate is not useful here: it just translates characters in the data 
according to the mapping, defined by operands 2 and 3.
1 => 5, and 0 to nothing.


<xsl:template match="//control[name='DelaySeconds']/numeric/scalemax">
<xsl:element name="{name(.)}">
        <xsl:value-of select="translate(string(.), '110', '5')" />


This replace the strings




but what I want is


I've tried using replace instead of translate but I get the error message

xmlXPathCompOpEval: function replace not found
XPath error : Unregistered function

Any ideas how to get round that?


On Fri, Jul 13, 2012 at 11:40 AM, stuart shepherd <jonny wark googlemail com


I've had a look at this and I'm not sure it does it exactly what I want.

My Main XML will look something like this


The control name is unique to that control, which is what I use to
identify it.
There will several controls in the XML files.
I need to change one or two of the max values of them, not all, and not
all to the same value.
The control element will contain other elements other than max.

After looking at your example I thought I could add an element to my sub
set XML that looks something like this.

    <name>ControlName</name> <!-- this will be the unique name of the
control whose values I want to change -->
   <max>100</max> <!-- the value I want to change -->

So what I want to do is look at the subset XML file and if there is an
override element find the control that it names and then overwrite the
value that is specified within the override element, it may be other
elements other than max, and maybe more than one. If it's easier I could
several overrides each one containing only a single override value.

Is this possible with XSLT?

Thank you for your help.


On Thu, Jul 12, 2012 at 7:07 PM, Piotr Sipika <piotreks optonline net>wrote:

On 07/12/2012 09:53 AM, stuart shepherd wrote:
Searching the web I've seen some examples in XSLT on how to
do something like this, but I have never used XSLT. Does anyone know if
there is a way to do this in XML.
XSLT is your best bet.

Here's a sample stylesheet which will:
 - change the name of all elements named 'old' to 'new'
 - change the name of all elements with an attribute named 'old' to 'new'

---- BEGIN stylesheet ----

<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet version="1.0"
  <xsl:output method="xml"/>

  <!-- Template matches every node and attribute-->
  <xsl:template match="node()|@*">

      <!-- if name of element is 'old' or element contains an attribute
           named 'old', replace it with 'new' -->
      <xsl:when test="@old or name(.)='old'">
        <xsl:variable name="old_node" select="node()"/>
        <xsl:variable name="old_value" select="./text()"/>
        <xsl:variable name="attributes" select="@*"/>
        <xsl:variable name="children" select="./*"/>

        <!-- create a 'new' element with attributes and children from
             the 'old' node, with its old value -->
        <xsl:element name="new">
          <xsl:copy-of select="$attributes|$children"/>
          <xsl:value-of select="$old_value"/>

      <!-- else, just copy the node/attributes -->
          <xsl:apply-templates select="node()|@*"/>


---- END stylesheet ----

Sample XML:
<?xml version="1.0" standalone="yes"?>
  <old attr="attr1">This used to be inside the old node
    <old_child1>This is old_child1</old_child1>
  <foo attr="attr2">This is just a middle element...</foo>
  <foo old="old attribute"/>

Output of stylesheet processing using xsltproc (libxml 20708, libxslt
10126 and libexslt 815):

$ xsltproc swap.xsl swap.xml
<?xml version="1.0"?>
  <new attr="attr1"><old_child1>This is old_child1</old_child1>This used
to be inside the old node
  <foo attr="attr2">This is just a middle element...</foo>
  <new old="old attribute"/>

You can modify the attribute match criteria (@old) to actually evaluate
its contents ([ old='old_parent']), etc...

Hope this helps.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>


xml mailing list
xml gnome org

End of xml Digest, Vol 99, Issue 7


Wolfgang Laun thalesgroup com - Software Technology
Thales Austria GmbH                      voice: +43 (0) 1 27711-5662
Scheydgasse 41                           fax:   +43 (0) 1 27711-1173
1210 Vienna, Austria           
    (FN 278233 t - Handelsgericht Wien/Commercial Court Vienna)

Attachment: wolfgang_laun.vcf
Description: wolfgang_laun.vcf

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]