【问题标题】:XSL: Matching template do not workXSL:匹配模板不起作用
【发布时间】:2013-12-20 14:43:03
【问题描述】:

这是我用于转换的 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> 

这是我要转换的 Source-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>

这是转换后的Target-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>

匹配对命名空间不起作用。如果我从 Source-XML 中删除 xmlns="http://anyurl.com" 我会得到我想要的结果。问题是我从外部系统获取 Source-XML,而我之前无法更改 Source-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>

【问题讨论】:

    标签: templates xslt namespaces


    【解决方案1】:

    假设您只需要像 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>
    

    如果您使用 XSLT 1.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>
    

    这应该是匹配的,但卡洛斯解决了另一个问题,即在正确的命名空间中创建新元素,所以你需要

    <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>
    

    【讨论】:

    • 非常感谢。这对我有用,我的问题就解决了。完美!
    猜你喜欢
    • 2011-03-08
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 2013-07-20
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多