Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 在XSL中使用style属性构建锚定标记_Xslt_Styles_Anchor - Fatal编程技术网

Xslt 在XSL中使用style属性构建锚定标记

Xslt 在XSL中使用style属性构建锚定标记,xslt,styles,anchor,Xslt,Styles,Anchor,我没有接受过XSL方面的正式培训,对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"&g

我没有接受过XSL方面的正式培训,对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>
<a style="display:block;width:500px;height:250px" id="player" href="ICT/LB_1152kbps.mp4"></a>

USER.PRIVATE_DATASOURCE.un:LU23921
测试标题
ICT/LB_1152kbps.mp4
500
250
我无法控制XML。我的意思是,XML是由工具生成的,我不能更改它。我现在要做的是编写XSL,它应该生成一个锚标记,如下所示:

<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>
<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书籍,或者看看类似的在线教程。在编写XSLT时,我强烈建议使用专用编辑器lixe。它不仅可以通过其自动模板功能和自动关闭标记来避免大量键入,还可以检查所有程序代码和XPath语法是否有效

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

<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处理器开始在根节点处理输入文档,然后使用
(或
)按您所说的步骤遍历元素。如果您希望匹配
的模板真正生效,则必须从文档元素上下文“移动”到该上下文:

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

如果您正在寻找一个好的起点,我建议您在图书馆里找一本XSLT书籍,或者看看类似的在线教程。在编写XSLT时,我强烈建议使用专用编辑器lixe。它不仅可以通过其自动模板功能和自动关闭标记来避免大量键入,还可以检查所有程序代码和XPath语法是否有效

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

<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处理器开始在根节点处理输入文档,然后使用
(或
)按您所说的步骤遍历元素。如果您希望匹配
的模板真正生效,则必须从文档元素上下文“移动”到该上下文:

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

以下是如何使用(属性值模板):

<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>
<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>

注意事项

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

在提供的XML文档上应用此转换时:

<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>
<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>

USER.PRIVATE_DATASOURCE.un:LU23921
测试标题
ICT/LB_1152kbps.mp4
500
250
生成所需的正确结果:

<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>
<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>

以下是如何使用(属性值模板):

<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>
<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>

注意事项

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

在提供的XML文档上应用此转换时:

<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>
<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>

USER.PRIVATE_DATASOURCE.un:LU23921
测试标题
ICT/LB_1152kbps.mp4
500
250
生成所需的正确结果:

<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>
<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>


感谢Thomas的回复。我尝试了一个非常简单的XSL,如下所示:但是,结果并不像预期的那样。感谢Thomas的回复。我尝试了一个非常简单的XSL,如下所示:但是,结果并不像预期的那样。如何在此处的问题中插入ID:如何在此处的问题中插入ID: