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
Templates 在不同的节点上应用模板';s级_Templates_Xslt_Xpath_Nodes - Fatal编程技术网

Templates 在不同的节点上应用模板';s级

Templates 在不同的节点上应用模板';s级,templates,xslt,xpath,nodes,Templates,Xslt,Xpath,Nodes,我尝试修改包含特定字符串的所有节点的QName: 并返回新的,而不返回 (例如,我有并希望呈现) 我有一个xsl:template用于第一级节点,但找不到如何将其应用于较低级别 e、 g.XML源 在这里,我想删除所有默认值` <?xml version="1.0" encoding="UTF-8"?> <Object> <DefaultID>XXXXXXX</DefaultID> <Defau

我尝试修改包含特定字符串的所有节点的QName: 并返回新的,而不返回

(例如,我有
并希望呈现

我有一个
xsl:template
用于第一级节点,但找不到如何将其应用于较低级别

e、 g.XML源 在这里,我想删除所有默认值`
<?xml version="1.0" encoding="UTF-8"?>
<Object>
  <DefaultID>XXXXXXX</DefaultID>
  <DefaultType>Random</DefaultType>
  <DefaultCustomer>
    <DefaultID>XXXXXXX</DefaultID>
    <DefaultName>John Doe</DefaultName>
    <DefaultAddress>33th Whitecaslt Blvd.</DefaultAddress>
    <DefaultNumber>+XX X XX XX XX XX</DefaultNumber>
  </DefaultCustomer>
</Object>

XXXXXXX
随机的
XXXXXXX
无名氏
怀特卡斯特大道33号。
+XX X XX XX XX
预期结果

XXXXXXX
随机的
XXXXXXX
无名氏
怀特卡斯特大道33号。
+XX X XX XX XX
我的XSL

我所拥有的

XXXXXXX
随机的
XXXXXXX
无名氏
怀特卡斯特大道33号。
+XX X XX XX XX
如您所见,模板与第二个节点的级别不匹配


我想我的问题来自我的
中的
XPath
的范围(顺便说一句,这似乎是无用的)?或者在这里,
不是最好的选择(同样的结果w/
)?

你想得太多了。试着简单地说:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="*[starts-with(name(), 'Default')]">
    <xsl:element name="{substring-after(name(), 'Default')}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element> 
</xsl:template>

</xsl:stylesheet>

@michael.hor257k抱歉,更好吗?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="@*|node()">
        <xsl:if test="string-length(.)>0">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>

    <xsl:template match="//*[contains(name(), 'Default')]">
        <xsl:element name="{substring-after(name(), 'Default')}">
            <xsl:copy select=".">
                <xsl:apply-templates match="//*[contains(name(), 'Default')]" />
            </xsl:copy>
        </xsl:element> 
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<Object>
  <ID>XXXXXXX</ID>
  <Type>Random</Type>
  <Customer>
    <DefaultID>XXXXXXX</DefaultID>
    <DefaultName>John Doe</DefaultName>
    <DefaultAddress>33th Whitecaslte Blvd.</DefaultAddress>
    <DefaultNumber>+XX X XX XX XX XX</DefaultNumber>
  </Customer>
</Object>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="*[starts-with(name(), 'Default')]">
    <xsl:element name="{substring-after(name(), 'Default')}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element> 
</xsl:template>

</xsl:stylesheet>