xml - XSLT 无法匹配具有特定命名空间的元素

标签 xml xslt namespaces uppercase

我有这个 XML 源文件:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?>
<MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/schemas/2005/gco" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd">
    <identificationInfo>
        <MD_DataIdentification>    
            <extent>
                <EX_Extent>
                    <geographicElement>
                        <EX_GeographicExtent>
                            <EX_GeographicBoundingBox>
                                <westBoundLongitude>
                                    <gco:Decimal>1</gco:Decimal>
                                </westBoundLongitude>
                                <eastBoundLongitude>
                                    <gco:Decimal>2</gco:Decimal>
                                </eastBoundLongitude>
                                <southBoundLatitude>
                                    <gco:Decimal>3</gco:Decimal>
                                </southBoundLatitude>
                                <northBoundLatitude>
                                    <gco:Decimal>4</gco:Decimal>
                                </northBoundLatitude>
                            </EX_GeographicBoundingBox>
                        </EX_GeographicExtent>
                    </geographicElement>
                    <temporalElement>
                        <EX_TemporalExtent>
                            <extent>
                                <gml:TimePeriod gml:id="tp1">
                                    <gml:begin>
                                        <gml:TimeIstant gml:id="ti1">
                                            <gml:timePosition>2007-12-01</gml:timePosition>
                                        </gml:TimeIstant>
                                    </gml:begin>
                                    <gml:end>
                                        <gml:TimeIstant gml:id="ti2">
                                            <gml:timePosition>2010-01-01</gml:timePosition>
                                        </gml:TimeIstant>
                                    </gml:end>
                                </gml:TimePeriod>
                            </extent>
                        </EX_TemporalExtent>
                    </temporalElement>
                </EX_Extent>
            </extent>
        </MD_DataIdentification>
    </identificationInfo>
</MD_Metadata>

我需要用这个简单的块替换块:
...
<gml:TimePeriod gml:id="TP1">
    <gml:beginPosition>2007-12-01</gml:beginPosition>
    <gml:endPosition>2010-01-01</gml:endPosition>
</gml:TimePeriod>
...

这是我的转变:
<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:gml="http://www.opengis.net/gml/3.2"
    xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
    xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
    xmlns="http://www.isotc211.org/schemas/2005/gmd"
    >

    <xsl:strip-space elements="*"/>

    <xsl:output indent="yes" encoding="UTF-8"/>

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

    <xsl:template match="gml:TimePeriod">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <beginPosition>
                <xsl:value-of select="gml:begin/gml:TimeIstant/gml:timePosition"/>
            </beginPosition>
            <endPosition>
                <xsl:value-of select="gml:end/gml:TimeIstant/gml:timePosition"/>
            </endPosition>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这是xmlns:gml="http://www.opengis.net/gml"样式表顶部的声明,但我认为这是命名空间的问题。
如果我在附近放置一个断点
<xsl:template match="gml:TimePeriod" exclude-result-prefixes="#all">
行,我从不输入该代码。
看来如果我需要通过<gmd:...>元素,一切正常,但是当我需要达到 <gml:...> 时(或任何其他与 gmd 不同的)元素,它不匹配。

-- 于 2014-04-15 更新 --

我忘了指定我还需要将 "tp1" 转换为大写<gml:TimePeriod gml:id="tp1"> 的属性值元素。在我的实际转型中我需要改变什么?

最佳答案

正如 Tomalak 在评论中提到的,问题的根本原因是您将不同的命名空间 URI 映射到 gml输入 XML 和样式表中的前缀,因此 XML 中的元素和 XSLT 想要匹配的元素不被视为相同。

关于你的补充:

I forgot to specify that I also need to convert to UPPER-CASE the "tp1" attribute value of <gml:TimePeriod gml:id="tp1"> element. What do I need to change on my actual transformation?



这应该只是添加一个额外的模板(一旦您对齐命名空间)并使用 XPath 2.0 upper-case 的问题。功能:
<xsl:template match="gml:TimePeriod/@gml:id">
  <xsl:attribute name="gml:id" select="upper-case(.)" />
</xsl:template>

这只会影响 gml:TimePeriod 的 ID元素,如果您想将所有 ID 都大写,那么只需将其设为 match="@gml:id"反而。

关于xml - XSLT 无法匹配具有特定命名空间的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23058271/

相关文章:

properties - Semantic Mediawiki 1.8.0 不显示自定义命名空间中的页面属性

xslt - 如何控制 XSLT 输出中的命名空间前缀(特别是默认命名空间)?

xml - 如何更改 XSLT 模板在 Visual Studio 2010 中使用的文件?

xml - 使用 XSL 从一个节点移动到另一个节点

css - 无法使用 CSS 和 XSL 设置背景图片

java - 使用 xpath 读取多个 XML 属性

c# - 如何解决命名空间冲突?

python - 如何在Python中检索xml标签的属性?

SQL Server - 返回 xml 列的 xml 子节点

java - 使用 Java 解密代码截断 C# 加密数据