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:_Xml_Xslt - Fatal编程技术网

有人知道如何使用xslt打印或获取XML的完整路径吗? 示例Xml:

有人知道如何使用xslt打印或获取XML的完整路径吗? 示例Xml:,xml,xslt,Xml,Xslt,下面是示例Xml <root name="Product"> <nodeOne name="Model Filter"/> <Folder name="Eye Care Hierarchies"> <nodeOne name="Eye Care Subcat-Manufacturer"/> <nodeOne name="Eye Care Subcat-Segment"/> &l

下面是示例Xml

<root name="Product">
    <nodeOne name="Model Filter"/>  
    <Folder name="Eye Care Hierarchies">
        <nodeOne name="Eye Care Subcat-Manufacturer"/>
        <nodeOne name="Eye Care Subcat-Segment"/>
    </Folder>
    <Folder name="Mega Hierarchies">
        <nodeOne name="Mega Hierarchies"/>
    </Folder>
</root>

预期结果: 它添加了一个名为“fullpath”的新属性


以下XSLT提供了非常接近您的目标。它可以被优化一点,但已经很晚了,该睡觉了

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

<xsl:output method="xml" encoding="utf-8" />

<xsl:template match="*">
    <xsl:copy>
            <xsl:apply-templates select="@*" mode="copy" />
            <xsl:apply-templates select="*" mode="copy">
                <xsl:with-param name="path" select="@name" />
            </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="copy">
    <xsl:param name="path" />
    <xsl:copy select=".">
        <xsl:attribute name="fullpath"><xsl:value-of select="$path" />
    </xsl:attribute>
    <xsl:apply-templates select="@*" mode="copy"/>
    <xsl:apply-templates select="*" mode="copy">
        <xsl:with-param name="path" select="concat($path, '.', @name)" />
    </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

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


您已经试过了吗?添加/计算
完整路径
的规则是什么?如图所示,您只是在一个级别上工作,还是真的需要它才能工作到任何深度?祝你好运。你好,深度必须是动态的。在本例中,我只输入了一个级别。水平可能会上升。基于完整路径必须使用xslt生成的级别。有什么办法吗?谢谢迈克尔,这帮了大忙。不客气。如果你能把答案记为正确的话,我将不胜感激。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="utf-8" />

<xsl:template match="*">
    <xsl:copy>
            <xsl:apply-templates select="@*" mode="copy" />
            <xsl:apply-templates select="*" mode="copy">
                <xsl:with-param name="path" select="@name" />
            </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="copy">
    <xsl:param name="path" />
    <xsl:copy select=".">
        <xsl:attribute name="fullpath"><xsl:value-of select="$path" />
    </xsl:attribute>
    <xsl:apply-templates select="@*" mode="copy"/>
    <xsl:apply-templates select="*" mode="copy">
        <xsl:with-param name="path" select="concat($path, '.', @name)" />
    </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

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