2017-02-22 55 views
2

我有一个文件,如下所示。 我需要处理文件并根据元素值= CT复制节点。我对于如何开始或从哪里开始感到失落。任何帮助,将不胜感激。尝试根据元素值选择节点

样式表:

<?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    > 
     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

     <xsl:template match="/"> 
     <feed> 
      <xsl:for-each select="web-export/run-date/pub-code/ad-type"> 
      <xsl:copy-of select="."/> 
      </xsl:for-each> 
     </feed> 
     </xsl:template> 
    </xsl:stylesheet> 

XML:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> 
<web-export> 
<run-date>02/01/2017 
<pub-code>News 
<ad-type>Legal Liner 
<cat-code>Legals</cat-code> 
<class-code>Legal</class-code> 
<subclass-code>Briefings</subclass-code> 
<placement-description></placement-description> 
<position-description></position-description> 
<subclass3-code></subclass3-code> 
<subclass4-code></subclass4-code> 
<order-number>0000023456</order-number> 
<FieldedDataSet> 
<State>CT</State> 
</FieldedDataSet> 
<ad-content><![CDATA[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque est. ]]></ad-content> 
</ad-type> 
</pub-code> 
<pub-code>News 
<ad-type>Legal Liner 
<cat-code>Legals</cat-code> 
<class-code>Legal</class-code> 
<subclass-code>Notices</subclass-code> 
<ad-number>0000</ad-number> 
<FieldedDataSet> 
<State>RI</State> 
</FieldedDataSet> 
<ad-content><![CDATA[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque est. ]]></ad-content> 
</ad-type> 
</pub-code> 
</run-date> 
</web-export> 

回答

0

工作,如果任何子元素的具有价值= “CT”

<xsl:template match="/"> 
    <feed> 
     <xsl:for-each select="web-export/run-date/pub-code/ad-type[descendant::FieldedDataSet/State[. = 'CT']]"> 
      <xsl:copy-of select="."/> 
     </xsl:for-each> 
    </feed> 
</xsl:template> 
+0

嗨Rupesh。感谢您及时的回复。这对我有用。我将如何定位该特定元素 - ?可能还有其他包含字符串'CT'的元素。我需要确保只有该元素的条目成功。谢谢。, – geektampa

+0

@geektampa我已经为你编辑了答案,现在它只能用于状态匹配。 – Rupesh

+0

谢谢。作品完美。这将派上用场。谢谢你今天教我新东西。 – geektampa

相关问题