Xml

XSLT 屬性轉換雙重打擊

  • May 10, 2019

我有一個要轉換為 HTML 的 XML 文件。我想省略很多不必要的屬性,但有兩個我想擷取如下:

來源

<element att1="yes" att2="no" att3="yes">Text</element>

期望的輸出

<span class="att1">Text</span>

因此,如果att1att2為“是”,則使用該屬性名稱創建一個類屬性;省略所有其他屬性,並將元素轉換為跨度。

在源文件中,att1並且att2應該是互斥的;如果一個是“是”,另一個可能不存在,也可能不存在。

這是一種方法(XSLT 2.0+):

<xsl:template match="element[(@att1, @att2)='yes']">
 <span class="{name((@att1, @att2)[.='yes'][1])}">
   <xsl:value-of select="."/>
 </span>
</xsl:template>

<xsl:template match="element">
 <!-- you haven't said what output you want if neither attribute is "yes" -->
</xsl:template>

引用自:https://unix.stackexchange.com/questions/518021