2017-10-19 108 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> 

也許不是最優雅之一,但它似乎工作。

仍然歡迎任何其他建議