0

这是我的 XSL:

    <xsl:template match="ImageView" >
    <view id ="3" src="{@src}" >
      <xsl:element name="form">
         <xsl:value-of select="@form"/>
     </xsl:element>
    </view>
  </xsl:template>

    <xsl:template match="TextView" >
    <view id ="5" src="{@src}" >
      <xsl:element name="form">
         <xsl:value-of select="@form"/>
     </xsl:element>
    </view>
</xsl:template>

我想知道在我的情况下是否可以使用通用模板?如果是,我该如何使用它?提前致谢。

编辑:

我的 XML:

               <ImageView
                android:id="@+id/3"
                android:src="@drawable/icon"
                custom:form="SOME_VALUE" />

                 <TextView
                android:id="@+id/5"
                android:src="@drawable/icon"
                custom:form="SOME_VALUE" />

我的预期结果应该是这样的:

    <ImageView >
     <view id="3" src="@drawable/icon">
        <form> SOME_VALUE</form>
     </view>
       </ImageView>

同样适用<TextView>。我将使用相同的srccustom:form。只会id不一样。

4

1 回答 1

0

您需要限定属性名称空间。

您还可以使用该xsl:copy指令来复制当前元素,因此两个视图都有一个通用模板。

然后我使用该substring-after函数来获取@id 属性的值。

希望这能让你走上正确的道路。

<xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:android="http://android"   
        xmlns:custom="http://custom" exclude-result-prefixes="android custom">
    <xsl:output indent="yes"/>

    <xsl:template match="ImageView|TextView">
        <xsl:copy>
            <view id="{substring-after(@android:id, '/')}" src="{@android:src}">
                <form><xsl:value-of select="@custom:form"/></form>
            </view>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
于 2013-06-25T13:16:08.763 回答