使用其他字符在标记内使用XSLT更改属性字符

时间:2013-11-13 16:06:53

标签: xml xslt

我可以使用XSLT将属性中的特定字符更改为XML中的字符。

我有这个XML

.... some data ....
<book attribute"test1/test2.test3"></book>
.... some data ...
<book attribute"test4/test5.test6"></book>
... some data ...

现在我想更改字符“/”和“。”到“_”,但仅限于标签簿。转换后,我需要像

一样编写XML
.... some data ....
<book attribute"test1_test2_test3"></book>
.... some data ...
<book attribute"test4_test5_test6"></book>
... some data ...

这可以简单地完成, 谢谢, eoglasi

2 个答案:

答案 0 :(得分:2)

这个XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="book/@attribute">
    <xsl:attribute name="attribute">
      <xsl:value-of select="translate(.,'./','__')"/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

复制输入XML中的所有内容'按原样'除了元素attribute中的属性book,其值被转换为将斜线和点更改为下划线。

您可以更改XPath book/@attribute以匹配需要以相同方式转换的任何其他属性 - 例如book/@attribute|person/@url也匹配元素url中的属性person

答案 1 :(得分:0)

这可以通过XSLT的translate()功能完成。有关此功能的说明,请参阅示例http://zvon.org/xxl/XSLTreference/Output/function_translate.html

一般语法如下:

translate('eoglasi', 'ae', 'AE')

结果是

'EoglAsi'