Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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

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将XML标记转换为任意位置的属性_Xml_Xslt_Xpath_Xslt 2.0 - Fatal编程技术网

使用XSLT将XML标记转换为任意位置的属性

使用XSLT将XML标记转换为任意位置的属性,xml,xslt,xpath,xslt-2.0,Xml,Xslt,Xpath,Xslt 2.0,我们有一种XML格式,它实际上只是带有一些额外gubbins的HTML。它定义了哪些HTML元素是可编辑的,以及哪些属性是可编辑的 这里有一个例子 <img src="images/placeholder.jpg" alt="Placeholder" width="600" height="250" border="0"> <var attr="src" /> <var attr="height" ok="15

我们有一种XML格式,它实际上只是带有一些额外gubbins的HTML。它定义了哪些HTML元素是可编辑的,以及哪些属性是可编辑的

这里有一个例子

<img src="images/placeholder.jpg" 
     alt="Placeholder" 
     width="600" 
     height="250" 
     border="0">
  <var attr="src" />
  <var attr="height" ok="150-300" />
</img>

遗憾的是,Alejandro给出的确切答案并没有立即奏效(仍然不知道为什么),但是他的回答和德米特里的回答似乎都做得很好:

< P>我会考虑建立一个局部变量(<代码> <代码>)当解析器到达
标记并在该变量内显式构造一个新的
,遍历它找到的任何
子变量(
任何人?)时,在变量耗尽时关闭该变量。。。然后把变量写出来

我不知道怎么做的是 指定一个可以 匹配
标签

使用

<xsl:template match="*[var]">

除了Dimitre对您的问题的确切答案外,其他方法是:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|var"/>
            <xsl:apply-templates select="node()[not(self::var)]"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="var"/>
    <xsl:template match="var[1]">
        <xsl:attribute name="editable">
            <xsl:for-each select="../var/@attr">
                <xsl:value-of
                     select="concat(.,
                                    substring(' ',
                                              1 div (position()!=last())))"/>
            </xsl:for-each>
        </xsl:attribute>
        <xsl:attribute name="constraints">
            <xsl:for-each select="../var/@ok">
                <xsl:value-of
                     select="concat(../@attr,':',.,
                                    substring(';',
                                              1 div (position()!=last())))"/>
            </xsl:for-each>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

输出:

<img src="images/placeholder.jpg" 
     alt="Placeholder" 
     width="600" 
     height="250" 
     border="0" 
     editable="src height" 
     constraints="height:150-300"></img>


问得好,+1。有关最自然的XSLT解决方案,请参见我的答案。:)+1,是的@Altreus,这意味着“匹配任何具有名为'var'的子元素的元素”。有时我希望可以将两个标记为可接受的答案。虽然这个答案正是我要问的,并且向我展示了一些我不知道的东西,我接受了更完整的答案,因为我有远见卓识,能够回答关于这个例子的所有其他问题,而我最终不可避免地会遇到这些问题:DKudos,因为我有远见卓识,能够举例说明我需要花费大量时间研究的所有其他事情,就像concat的东西:)我接受了这个答案,因为它的完整性。嗯,我不得不尝试一下,因为我的简单示例不起作用。但是我认为我的思路是正确的:)@Altreus:问题可能是因为
var
元素不是第一个子元素,包括它们在(X)HTML中有意义的纯空白文本节点。简单的解决方案包括对身份规则进行轻微修改。检查我的编辑。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|var"/>
            <xsl:apply-templates select="node()[not(self::var)]"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="var"/>
    <xsl:template match="var[1]">
        <xsl:attribute name="editable">
            <xsl:for-each select="../var/@attr">
                <xsl:value-of
                     select="concat(.,
                                    substring(' ',
                                              1 div (position()!=last())))"/>
            </xsl:for-each>
        </xsl:attribute>
        <xsl:attribute name="constraints">
            <xsl:for-each select="../var/@ok">
                <xsl:value-of
                     select="concat(../@attr,':',.,
                                    substring(';',
                                              1 div (position()!=last())))"/>
            </xsl:for-each>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>
<img src="images/placeholder.jpg" 
     alt="Placeholder" 
     width="600" 
     height="250" 
     border="0" 
     editable="src height" 
     constraints="height:150-300"></img>