0

How can I replace special characters and spaces in an attribute? I tried various regular expressions but none of them worked as aspected.

<pattern id="setElementId">
  <rule context="*[contains(@class, ' domain/element ') and boolean(@id)]">
    <!-- Works, replaces 'a' -->
    <let name="reqId" value="replace(@id, '[a]', '')"/>
    <assert test="@id=$reqId" sqf:fix="setId">
      The attribute "id" must comply with the given rules: "<value-of select="$reqId"/>" 
    </assert>
    <sqf:fix id="setId">
      <sqf:description>
       <sqf:title>Set "id" to "<value-of select="$reqId"/>"</sqf:title>
       <sqf:p>Set "id" to the calculated value.</sqf:p>
      </sqf:description>
      <sqf:replace match="@id" node-type="attribute" target="id" select="$reqId"/>
    </sqf:fix>
  </rule>
</pattern>
Stefan Jung
  • 1,209
  • 11
  • 20
  • Could you please provide a simple example of the required xml input and output? It is not clear what kind of replacement you want to do. – sergioFC Oct 13 '15 at 19:29
  • 1
    Note that if you want to remove every character except characters between a-z or A-Z or 0-9 you can use something like this replace(@id, '[^a-zA-Z]', '') . – sergioFC Oct 13 '15 at 19:57

2 Answers2

0

Your last efforts weren't remorsed I think.

Check the CSS codings:

Style {template.css}

Remove that from the end user's example and you'll have your answer!

<sqf:replace match="@id" node-type="attribute" target="id" select="$reqId"/>
</sqf:fix>

That didn't return the side value.

coins
  • 1
  • To be honest, I do not understand your answer. Which CSS codings do you mean? The given example works, but only replaces 'a' with ''. But I want to replace, for example '.', ',', '\', '/' with ''. – Stefan Jung Oct 13 '15 at 11:24
  • BTW: The `` is a [Schematron QuickFix rule](http://www.schematron-quickfix.com/) – Stefan Jung Oct 13 '15 at 11:28
0

I answer my own question using the suggested replace statement by sergioFC.

<pattern id="setElementId">
  <rule context="*[contains(@class, ' topic/dlentry ') and boolean(@id) and descendant-or-self::*[contains(@class, ' ui-d/uicontrol ')]]" role="info">
    <let name="reqId" value="descendant-or-self::*[contains(@class, ' ui-d/uicontrol ')]"/>
    <let name="reqId" value="replace($reqId, 'ä', 'ae')"/>
    <let name="reqId" value="replace($reqId, 'ö', 'oe')"/>
    <let name="reqId" value="replace($reqId, 'ü', 'ue')"/>
    <let name="reqId" value="replace($reqId, 'Ä', 'ue')"/>
    <let name="reqId" value="replace($reqId, 'Ö', 'ue')"/>
    <let name="reqId" value="replace($reqId, 'Ü', 'ue')"/>
    <let name="reqId" value="replace($reqId, 'ß', 'ss')"/>
    <let name="reqId" value="replace($reqId, '[^0-9a-zA-Z]', '')"/>
    <assert test="@id=$reqId" sqf:fix="setId" role="warning">
      The attribute "id" must comply with the given rules: "<value-of select="$reqId"/>" 
    </assert>
    <sqf:fix id="setId">
      <sqf:description>
       <sqf:title>Set "id" to "<value-of select="$reqId"/>"</sqf:title>
       <sqf:p>Set "id" to the calculated value.</sqf:p>
      </sqf:description>
      <sqf:replace match="@id" node-type="attribute" target="id" select="$reqId"/>
    </sqf:fix>
  </rule>
</pattern>

Using this pattern, I can generate id attributes for <dlentry> elements based on the value of the descendant <uicontrol> element.

Stefan Jung
  • 1,209
  • 11
  • 20
  • It seems you are not familiar with regex. `[]` is used to match any character between the brackets, so, for example the regex "a[012]b" matches "a0b", "a1b" or "a2b". `[^]` is used to match any character that is not between the brackets. If you want a regex to match a single character you don't need to use brackets, so, you can use `'ä'` instead of `'[ä]'` – sergioFC Oct 14 '15 at 17:07
  • Thanks, @sergioFC. I modified the snippet. – Stefan Jung Oct 15 '15 at 15:44
  • You're welcome, note that you can accept your own answer by checking the tick. – sergioFC Oct 17 '15 at 18:15