Domanda

i found a lot about translating specific Elements/attributes through XSLT function translate(source, sourceChars, outputChars) so for translate("čašaž","čšž", "csz") = casaz

I need XSLT template, which translate each node and each attribute. I don't know structure of source XML, so its have to be universal nonindependent on attribute or element names and values.

I am looking for something like this pseudo transformation:

  <xsl:template match="@*">
    <xsl:copy>
        <xsl:apply-templates select="translate( . , "čžš","czs")"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="translate( . , "čžš","czs")"/>
    </xsl:copy>
  </xsl:template>
È stato utile?

Soluzione

You can write templates for those elements containing data you want to normalize, below I do that for attribute values, text node, comment nodes and processing instruction data.

<xsl:param name="in" select="'čžš'"/>
<xsl:param name="out" select="'czs'"/>

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

<xsl:template match="@*">
  <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
    <xsl:value-of select="translate(., $in, $out)"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select="translate(., $in, $out)"/>
</xsl:template>

<xsl:template match="comment()">
  <xsl:comment>
    <xsl:value-of select="translate(., $in, $out)"/>
  </xsl:comment>
</xsl:template>

<xsl:template match="processing-instruction()">
  <xsl:processing-instruction name="{name()}">
    <xsl:value-of select="translate(., $in, $out)"/>
  </xsl:processing-instruction>
</xsl:template>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top