XSLT使用选择存在节点

时间:2017-10-19 10:03:27

标签: xml xslt

我想使用“选择”检查节点是否存在,然后在其中提取文本。如果不是,则应插入字符串。 这就是我所做的:

<?xml version="1.0" encoding="UTF-8"?>
<gmi:MI_Metadata xmlns:gmi="http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi" xmlns:xsi="https://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" xmlns:gss="http://www.isotc211.org/2005/gss" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" gco:isoType="gmd:MD_Metadata" xsi:schemaLocation="http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi/gmi.xsd">
<gmd:pointOfContact>
                <gmd:CI_ResponsibleParty>
                    <gmd:individualName>
                        <gco:CharacterString>Pippo</gco:CharacterString>
                    </gmd:individualName>
                </gmd:CI_ResponsibleParty>
</gmd:pointOfContact>
</gmi:MI_Metadata>

在此示例中,要搜索的整个节点为:“gmd:pointOfContact / gmd:CI_ResponsibleParty / gmd:individualName / gco:CharacterString”

我的输出应该是一个txt文件,如下一行:

负责任:Pippo;

当节点存在且字符串为“Pippo”时。

不负责任:;

节点不存在时。

你能告诉我为什么我无法得到这个结果吗?

这是我正在使用的xml的提取部分:

<gmd:pointOfContact>
                <gmd:CI_ResponsibleParty>
                    <gmd:organisationName>
                        <gco:CharacterString>HOUSE</gco:CharacterString>
                    </gmd:organisationName>
                </gmd:CI_ResponsibleParty>
</gmd:pointOfContact>
</gmi:MI_Metadata>

在某些情况下,我可以找到这样的事情:

$("#modal_scroll{{ obj.id }}").off('click');

且标签“individualName”缺失

3 个答案:

答案 0 :(得分:0)

我会尝试

<xsl:template match="gmd:individualName">
   <xsl:choose>
        <!-- check if gco:CharacterString exist -->
        <xsl:when test="gco:CharacterString">    
               <xsl:text>Responsible: </xsl:text>
               <xsl:apply-templates select="gco:CharacterString"/>
               <xsl:text>;</xsl:text>
        </xsl:when>
        <xsl:otherwise>NO Responsible: ;</xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="gmd:individualName">
   <xsl:choose>
        <!-- check if gco:CharacterString contains text -->
        <xsl:when test="string-length(gco:CharacterString) &gt; 0">    
               <xsl:text>Responsible: </xsl:text>
               <xsl:apply-templates select="gco:CharacterString"/>
               <xsl:text>;</xsl:text>
        </xsl:when>
        <xsl:otherwise>NO Responsible: ;</xsl:otherwise>
    </xsl:choose>
</xsl:template>

答案 1 :(得分:0)

如果您只输出文本,则不应该尝试使用xsl:copy复制元素,因此您不需要具有“输出”模式的模板。此外,不需要“抑制节点”的模板,因为XSLT的内置模板已经做了同样的事情。

尝试使用此XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:gmd="http://www.isotc211.org/2005/gmd"
xmlns:gts="http://www.isotc211.org/2005/gts"
xmlns:gco="http://www.isotc211.org/2005/gco"
xmlns:gml="http://www.opengis.net/gml"
xmlns:geonet="http://www.fao.org/geonetwork">
<xsl:output method="text" encoding="utf-8" />

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

<xsl:template match="gmd:pointOfContact/gmd:CI_ResponsibleParty">
    <xsl:choose>
        <xsl:when test="gmd:individualName/gco:CharacterString">    
               <xsl:text>Responsible: </xsl:text>
               <xsl:value-of select="gmd:individualName/gco:CharacterString" />
               <xsl:text>;</xsl:text>
        </xsl:when>
        <xsl:otherwise>NO Responsible: ;</xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

答案 2 :(得分:0)

我想我找到了解决方案,从@jonycleva建议开始:

<xsl:template match="gmd:pointOfContact/gmd:CI_ResponsibleParty"> <xsl:choose> 
<xsl:when test="gmd:individualName/gco:CharacterString"> <xsl:text>Responsible: </xsl:text> 
<xsl:value-of select="gmd:individualName/gco:CharacterString"/> 
<xsl:text>;</xsl:text> 
</xsl:when> 
<xsl:otherwise>Responsible: ;</xsl:otherwise> 
</xsl:choose> 
</xsl:template>

也许不是最优雅的,但似乎有效。

仍欢迎任何其他建议