我想檢查節點是否存在使用「選擇」,然後提取其中的文本。如果不是,應該插入一個字符串。 這裏是我做過什麼: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感謝您的建議。我添加了一個例子。 –