【问题标题】:Build anchor tag with style attribute in XSL在 XSL 中构建具有样式属性的锚标记
【发布时间】:2012-12-24 07:37:57
【问题描述】:

我没有接受过关于 XSL 的正式培训,而且对它完全陌生。基本上我有一个如下的 XML 文件:

<document document="wpc_article_video_qp">
  <properties>
    <property type="name" prop_ns="http://sapportals.com/xmlns/cm" prop_name="displayname"/>
    <property type="createdBy">USER.PRIVATE_DATASOURCE.un:LU23921</property>
    <property type="includeInRSS" prop_ns="wpc_wcm" prop_name="wpc_wcm_rss"/>
    <property type="displayNewIcon" prop_ns="wpc_wcm" prop_name="wpc_wcm_new"/>
  </properties>
  <elements>
    <element type="videotitle">TestTitle</element>
    <element type="videopath">ICT/LB_1152kbps.mp4</element>
    <element type="videowidth">500</element>
    <element type="videoheight">250</element>
  </elements>
  <relatedlinks/>
  <relatedfiles/>
</document>

我无法控制 XML。我的意思是 XML 是由工具生成的,我无法更改它。我现在要做的是编写 XSL,它应该生成一个锚标记,如下所示:

<a style="display:block;width:500px;height:250px" id="player" href="ICT/LB_1152kbps.mp4"></a>

href、width 和 height 分别从 "videopath"、"videowidth" 和 "videoheight" XML 元素中获取。

我尝试在这个网站和其他一些网站上进行搜索,但正如我所说,由于我对 XSL 完全陌生,我真的不知道从哪里开始。任何帮助将不胜感激。

【问题讨论】:

    标签: xslt styles anchor


    【解决方案1】:

    如果您正在寻找一个好的起点,我建议您在图书馆里找一本 XSLT 书籍,或者看看一些在线教程,例如 the one by Zvon。在编写 XSLT 时,我强烈推荐使用专门的编辑器 lixe oXygen。它不仅可以通过自动完成功能和自动关闭标签使您免于输入大量内容,还可以检查所有程序代码和 XPath 语法是否有效。

    这里的问题是:您可能会生成一个完整的 HTML 文档,而不仅仅是一个锚标记。所以,这里有一个模板,它通过匹配&lt;element&gt; 元素来生成锚标记,但必须集成到整个样式表中:

    <xsl:template match="element">
      <a style="display:block;
                width:{element/@videowidth}px;
                height:{element/@videoheight}px" 
         id="player" href="{element/@videopath}"></a>
    </xsl:template>
    

    编辑: 如果您真的想要一个只包含锚标记的输出文档:XSLT 处理器开始处理根节点处的输入文档,然后按照您使用 @ 告诉它的元素逐步执行987654326@(或&lt;xsl:for-each&gt;)。如果您希望匹配&lt;element&gt; 的模板真正生效,您必须从文档元素上下文“移动”到该上下文:

    <xsl:template match="/">
      <xsl:apply-templates select="document/elements/element"/>
    </xsl:template>
    

    【讨论】:

    • 感谢托马斯的回复。我尝试了一个非常简单的 XSL,如下所示: w3.org/1999/XSL/Transform"> 但是,结果并不如预期。
    【解决方案2】:

    这里是如何做到这一点,使用AVT(属性值模板):

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="elements">
         <xsl:copy>
           <a style="display:block;
                     width:{*[@type='videowidth']}px;
                     height:{*[@type='videoheight']}px"
                     id="player" href="{*[@type='videopath']}"></a>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    请注意

    上面的 CSS 属性都在一个新的行上 -- 这样做是为了便于阅读。在真正的转换中,人们可能希望在不插入空格的情况下保留它们——以在没有空格的情况下产生想要的结果。

    当此转换应用于提供的 XML 文档时:

    <document document="wpc_article_video_qp">
      <properties>
        <property type="name" prop_ns="http://sapportals.com/xmlns/cm" prop_name="displayname"/>
        <property type="createdBy">USER.PRIVATE_DATASOURCE.un:LU23921</property>
        <property type="includeInRSS" prop_ns="wpc_wcm" prop_name="wpc_wcm_rss"/>
        <property type="displayNewIcon" prop_ns="wpc_wcm" prop_name="wpc_wcm_new"/>
      </properties>
      <elements>
        <element type="videotitle">TestTitle</element>
        <element type="videopath">ICT/LB_1152kbps.mp4</element>
        <element type="videowidth">500</element>
        <element type="videoheight">250</element>
      </elements>
      <relatedlinks/>
      <relatedfiles/>
    </document>
    

    产生了想要的正确结果:

    <elements>
       <a style="display:block;width:500px;height:250px" id="player" href="ICT/LB_1152kbps.mp4"/>
    </elements>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多