我想检查节点是否存在使用“选择”,然后提取其中的文本。如果不是,应该插入一个字符串。 这里是我做过什么:XSLT使用选择节点的存在
<?xml version="1.0" encoding="UTF-8"?>
<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" />
<!-- identity templates walks tree and suppresses nodes with no template -->
<xsl:template match="node()|@*">
<xsl:apply-templates select="node()|@*"/>
</xsl:template>
<!-- output only on nodes we select -->
<xsl:template match="node()|@*" mode="output">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="output"/>
</xsl:copy>
</xsl:template>
<xsl:template match="gmd:pointOfContact/gmd:CI_ResponsibleParty">
<xsl:choose>
<xsl:when test="gmd:individualName/gco:CharacterString">
<xsl:text>Responsible: </xsl:text>
<xsl:apply-templates mode="output"/>
<xsl:text>;</xsl:text>
</xsl:when>
<xsl:otherwise>NO Responsible: ;</xsl:otherwise>
</xsl:choose>
</xsl:template>
在这个例子中要搜索的整个节点是: “GMD:pointOfContact/GMD:CI_ResponsibleParty/GMD:INDIVIDUALNAME/GCO:CharacterString”
我的输出应该是一个txt文件,如下所示:
责任人:Pippo;
当节点存在且字符串为“Pippo”时。
不负责任;
当节点不存在时。
你能告诉我为什么我不能得到这个结果吗?
这里是一个XML的提取部分我用:
<?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:organisationName>
<gco:CharacterString>HOUSE</gco:CharacterString>
</gmd:organisationName>
</gmd:CI_ResponsibleParty>
</gmd:pointOfContact>
</gmi:MI_Metadata>
和标记“INDIVIDUALNAME “缺少
您是否可以编辑您的问题以显示您的输入XML示例?另外,你还可以显示你目前正在获得的输出吗?谢谢。 –
@Tim C感谢您的建议。我添加了一个例子。 –