2017-10-19 106 views
0

我想检查节点是否存在使用“选择”,然后提取其中的文本。如果不是,应该插入一个字符串。 这里是我做过什么: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 “缺少

+1

您是否可以编辑您的问题以显示您的输入XML示例?另外,你还可以显示你目前正在获得的输出吗?谢谢。 –

+0

@Tim C感谢您的建议。我添加了一个例子。 –

回答

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> 
+0

@jonicleva感谢您的回复,但不幸的是,它仍然无法正常工作。我在一个xml上测试了它,其中负责名称存在 –

+0

Hi @ sylar_80,我更新了代码,最好使用“apply-templates”而不是“value-of”,因为“value-of”不处理孩子,它只显示文字。 但你如何描述它,也服务于,我希望能得到帮助。 – jonycleva

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> 
+0

嗨,你的建议如果与我提取的例子一起使用。但是,如果应用于整个XML,我将输出包含在.xml中的整个文本。 :(这就是为什么我使用“压制模式”和xsl:copy的方法 –

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> 

也许不是最优雅之一,但它似乎工作。

仍然欢迎任何其他建议