Page 1 of 1

Search and replace: Use XPath in replace expression

Posted: Thu Sep 30, 2010 12:53 pm
by ckirchho
Hello,

I have the following exemplary tei tagging structure:

Code: Select all

<text type="art_miscellanea" n="[212.1]" xml:id="ar212mi01">
...
<body>
<div type="misc_undef">...</div>
<div type="misc_undef">...</div>
I would like to insert an xml:id attribute to those div tags that consist of the xml:id attribute of the text tag plus an underscore and an counter.

Searching with an

Code: Select all

XPath //text[@type='art_miscellanea']/body/div[@type='misc_undef']
for the text

Code: Select all

type="misc_undef"
works. It would find the seacrhed text in children of those divs as well, but there are no children with such a type attribute.

Now I would like to replace that text with

Code: Select all

type="misc_undef" xml:id="<expression>"
And the expression has to be something like

Code: Select all

concat(parent::body/parent::text/@xml:id, '_', counter())
The counter could even be derived from the position of the div within the body tag.

Is there anything like this possible? Writing a transformation to do this would be an alternative, but I was wondering if XPath in replace expressions is supported somehow...

Best regards,

Christian Kirchhoff

Re: Search and replace: Use XPath in replace expression

Posted: Thu Sep 30, 2010 2:32 pm
by adrian
Hello,

Unfortunately you cannot evaluate that XPath expression in context to use its result as a replace pattern. There is a messy way to do this with regular expressions but to be safe you should use an XSLT stylesheet or XQuery.

Regards,
Adrian