gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:59:22 25 4
gpt4 key购买 nike

我有这个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com