Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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
Templates XSL:匹配模板不起作用_Templates_Xslt_Namespaces - Fatal编程技术网

Templates XSL:匹配模板不起作用

Templates XSL:匹配模板不起作用,templates,xslt,namespaces,Templates,Xslt,Namespaces,这里是转换的XSL <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> <xsl:strip-space elements="*" /> <xsl:template match="@*|node()">

这里是转换的XSL

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="EL3">
    <te>ABC</te>
</xsl:template> 
这就是我要转换的源XML

<EL1 xmlns="http://anyurl.com" language="2">
<EL2>
    <set1>
        <value1>12</value1>
        <value2>34</value2>
        <value3>45</value3>         
    </set1>
</EL2>
<EL2>
    <set1>
        <value1>AB</value1>
        <value2>CD</value2>
        <value3>EF</value3>
        <EL3>
            <value1>AB</value1>
            <value2>CD</value2>
            <value3>EF</value3>
        </EL3>
    </set1>
</EL2>
这是转换后的目标XML

<EL1 xmlns="http://anyurl.com" language="2">
<EL2>
    <set1>
        <value1>12</value1>
        <value2>34</value2>
        <value3>45</value3>         
    </set1>
</EL2>
<EL2>
    <set1>
        <value1>AB</value1>
        <value2>CD</value2>
        <value3>EF</value3>
        <EL3>
            <value1>AB</value1>
            <value2>CD</value2>
            <value3>EF</value3>
        </EL3>
    </set1>
</EL2>
与命名空间相关的匹配不起作用。如果我删除xmlns=http://anyurl.com 从源XML我得到了我想要的结果。问题是,我从外部系统获取源XML,以前无法更改源XML。如何编辑XSL以获得这样的等待结果

<EL1 language="2">
   <EL2>
      <set1>
         <value1>12</value1>
         <value2>34</value2>
         <value3>45</value3>
      </set1>
   </EL2>
   <EL2>
      <set1>
         <value1>AB</value1>
         <value2>CD</value2>
         <value3>EF</value3>
         <te>ABC</te>
      </set1>
   </EL2>
</EL1>

假设像Saxon 9或XmlPrime这样的XSLT 2.0处理器,您只需要

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://anyurl.com">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="EL3">
    <te>ABC</te>
</xsl:template>
如果使用XSLT1.0处理器,则需要

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:df="http://anyurl.com" exclude-result-prefixes="df">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="df:EL3">
    <te>ABC</te>
</xsl:template>
这对于匹配应该已经足够了,但是Carlos解决了另一个问题,即在正确的名称空间中创建新元素,因此您需要

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://anyurl.com" xmlns="http://anyurl.com">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="EL3">
    <te>ABC</te>
</xsl:template>
分别

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:df="http://anyurl.com" exclude-result-prefixes="df" xmlns="http://anyurl.com">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="df:EL3">
    <te>ABC</te>
</xsl:template>

非常感谢。这对我很有效,我的问题也解决了。完美的