【问题标题】:XSLT transform single attribute to elementXSLT 将单个属性转换为元素
【发布时间】:2013-06-18 09:43:38
【问题描述】:

我正在尝试将单个属性转换为元素。 我使用的 XSL 是:

   <xsl:template match="TextView/@*">
   <xsl:element name="{name()}">
   <xsl:value-of select="."/>
   </xsl:element>
   </xsl:template>

我的 xml:

    <TextView
     android:id="@+id/ti"
     style="@style/HTxt"
     android:text="@string/ti"
     custom:attribute="name" />

上述 XSL 将所有属性转换为元素。但我只想转换'custom:attribute'并忽略其他。我怎样才能做到这一点?提前致谢。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    @*替换为@custom:attribute
    因此尝试:

    <xsl:template match="TextView/@custom:attribute">
            <xsl:element name="{name()}">
                <xsl:value-of select="."/>
            </xsl:element>
    </xsl:template>
    

    但注意customnamespace prefix。您必须使用与 XML 中相同的命名空间 URL 将其添加到 xsl:stylesheet。 类似的东西:

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

    【讨论】:

    • hr_117 谢谢..但是即使显示了其他属性..我怎样才能删除除 custom:attribute 之外的所有内容???
    • 我假设您的 xslt 中有一个身份转移模板。因此广告一个新模板来忽略 TextView 的属性 (&lt;xsl:template match="TextView/@*" /&gt;)
    • @hr_117 请注意,您需要将明确的priority="..." 添加到两个模板中,因为默认情况下TextView/@custom:attributeTextView/@* 具有相同的优先级。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多