Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
Css xsl检索其他属性值并将值附加到一个属性中_Css_Xml_Xslt_Xpath - Fatal编程技术网

Css xsl检索其他属性值并将值附加到一个属性中

Css xsl检索其他属性值并将值附加到一个属性中,css,xml,xslt,xpath,Css,Xml,Xslt,Xpath,开始: <test style="font:2px;color:#FFFFFF" bgcolor="#CCCCCC" TOPMARGIN="5">style</test> 风格 使用XSLT/XPATH,我复制了文档中的所有内容 <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </

开始:

<test style="font:2px;color:#FFFFFF" bgcolor="#CCCCCC" TOPMARGIN="5">style</test>
风格
使用XSLT/XPATH,我复制了文档中的所有内容

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

但我不确定如何使用XSLT/XPATH获得此结果:

<test style="background-color:#CCCCCC; margin-top:1;font:2px;color:#FFFFFF">style</test>
风格
我想我在XPATH方面失败了。这是我仅检索bgColor的尝试:

<xsl:template match="@bgColor">
 <xsl:attribute name="style">
   <xsl:text>background-color:</xsl:text>
   <xsl:value-of select="."/>
   <xsl:text>;</xsl:text>
   <xsl:value-of select="../@style"/>
 </xsl:attribute>
</xsl:template>

背景色:
;

不幸的是,在原始文档中,当样式放置在bgColor之后时,即使这样也会中断。如何将这些不推荐使用的属性值附加到一个内联样式属性中?

可能不是最好的方法,但它可以工作:

<xsl:template match="test">
    <xsl:element name="{name()}">
        <xsl:apply-templates select="@*[name() != 'bgcolor']"/>   
    </xsl:element>
</xsl:template>    

<xsl:template match="@*">
    <xsl:copy/>
</xsl:template>

<xsl:template match="@style">
    <xsl:attribute name="style">
        <xsl:value-of select="."/>
        <xsl:text>;background-color:</xsl:text>
        <xsl:value-of select="../@bgcolor"/>
    </xsl:attribute>
</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:template match="/*">
  <test style="{@style};background-color:{@bgcolor};margin-top:{@TOPMARGIN}">
   <xsl:value-of select="."/>
  </test>
 </xsl:template>
</xsl:stylesheet>
<test style="font:2px;color:#FFFFFF"
      bgcolor="#CCCCCC" TOPMARGIN="5">style</test>
<test style="font:2px;color:#FFFFFF;background-color:#CCCCCC;margin-top:5">style</test>

应用于提供的XML文档时

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <test style="{@style};background-color:{@bgcolor};margin-top:{@TOPMARGIN}">
   <xsl:value-of select="."/>
  </test>
 </xsl:template>
</xsl:stylesheet>
<test style="font:2px;color:#FFFFFF"
      bgcolor="#CCCCCC" TOPMARGIN="5">style</test>
<test style="font:2px;color:#FFFFFF;background-color:#CCCCCC;margin-top:5">style</test>
风格
生成所需的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <test style="{@style};background-color:{@bgcolor};margin-top:{@TOPMARGIN}">
   <xsl:value-of select="."/>
  </test>
 </xsl:template>
</xsl:stylesheet>
<test style="font:2px;color:#FFFFFF"
      bgcolor="#CCCCCC" TOPMARGIN="5">style</test>
<test style="font:2px;color:#FFFFFF;background-color:#CCCCCC;margin-top:5">style</test>
风格

解释:使用

好问题,+1。请参阅我的答案,了解使用XSLT的一个优秀功能—属性值模板(AVT)的完整、简短且简单的解决方案。在该解决方案中,封装了整个文档,文档中每个节点的文本值都显示在中。但是,它确实适用于问题中所述的单个测试实例。假设我想将其应用于每个元素,而不仅仅是“测试”?@Moyler:在这种情况下,您必须使用
xsl:element
xsl:attribute
。提出一个新问题,并提出完善的要求,我很乐意回答。