Reusing string in XML

Asked

Viewed 79 times

4

Ex.:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="primeiro_nome">Bigown</string>
   <string name="nome_completo">{primeiro nome} Silva</string>
</resources>

You can reuse the first string name in the string below directly in xml?

2 answers

1

Just as an observation I did not test this directly with the Android SDK, but use with Xamarin and is working perfectly for me

You can declare new entities in your XML to be able to reuse texts, so it is easier to update certain values that repeat, I am using as follows

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
    <!ENTITY telefone "(12) 3456 7890">
    <!ENTITY site "http://www.meusite.com.br">
]>
<resources>
    <string name="contato">Entre em contato com o nosso suporte pelo telefone &telefone;</string>
    <string name="saibamais">Conheça mais sobre o nosso produto visitando no website &site;.</string>
</resources>

Note that in XML I declared an entity called telefone, and she’s used as &telefone; in XML

  • I tested here on Android SDK and apparently worked as expected, good technique I will pass to use it. + 1.

0

To copy the value of one element into another you need to use XSLT and use the declaration <xsl:copy-of select="$NOME_DO_ELEMENTO_COPIADO" />.

The example you set would look like this:

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<resources> 
    <string name="primeiro_nome">Bigown</string> 
    <string name="nome_completo"><xsl:copy-of select="$primeiro_nome" /> Silva</string> 
</resources>

The complete documentation for reference is on the page http://www.w3schools.com/xsl/el_copy-of.asp

  • could not, I am using in xml for android :/

  • @Josinaldo We hadn’t declared the namespace xsl:stylesheet in the file header, and so xsl:copy-of was not being recognized. I edited my response by adding this namespace. It might work now. If it still doesn’t work, we still have the option to declare these XML transformation definitions in an external file and refer it to your resource file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.