2016-03-09 21 views
0

我有以下結構...

<story><p>£££ Some Name $$$story text text text etc</p><p>more text text text etc</p></story> 

我一直在嘗試使用子,後選擇$$$後的文字,並計劃在一串接<p>到開始。但是我使用這種方法丟失了所有的標籤。我很努力地用正確的語法創建一個模板解決方案來做到這一點。 所需的輸出 -

<story><p>story text text text etc</p><p>more text text text etc</p></story> 
可以

任何人的幫助。感謝安吉拉

<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" omit-xml-declaration="yes" indent="yes"/> 

    <xsl:template match="VirtualComCMS"> 
    <xsl:apply-templates select="VirtualComCMS"/> 
    </xsl:template> 

    <xsl:template match="VirtualComCMS"> 

    <xsl:for-each select="articles/article"> 
     <Article> 
     <xsl:element name="StoryID"> 
      <xsl:value-of select="@id" /> 
     </xsl:element> 
     <Name> 
      <xsl:value-of select="Name"/> 
     </Name> 
     <ModificationDate> 
      <xsl:value-of select="ModificationDate"/> 
     </ModificationDate> 
     <CreationDate> 
      <xsl:value-of select="CreationDate"/> 
     </CreationDate> 
     <Headline> 
      <xsl:value-of select="Headline"/> 
     </Headline> 
     <Subheadline> 
      <xsl:value-of select="Subheadline"/> 
     </Subheadline> 
     <Story> 
      <xsl:template match="@*|node()"> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()"/> 
      </xsl:copy> 
      </xsl:template> 

      <xsl:template match="p/text()[contains(., '$$$')]"> 
      <xsl:value-of select="substring-after(., '$$$')"/> 
      </xsl:template> 
       <xsl:copy-of select="Body/div/node()"/> 

     </Story> 
     <Summary> 
      <xsl:value-of select="Summary"/> 
     </Summary> 
     <Keywords> 
      <xsl:value-of select="Keywords"/> 
     </Keywords> 
     <Latitude> 
      <xsl:value-of select="latitude"/> 
     </Latitude> 
     <Longitude> 
      <xsl:value-of select="longitude"/> 
     </Longitude> 
     <Categories> 
      <xsl:value-of select="Categories" /> 
     </Categories> 
     <!-- <Categories> 
      <xsl:value-of select="substring-before(substring-after(Categories, '/'),'/')" /> 
     </Categories> 
     <SubCategories> 
      <xsl:value-of select="substring-after(substring-after(Categories, '/'),'/')" /> 
     </SubCategories>--> 
     <Priority> 
      <xsl:value-of select="Priority"/> 
     </Priority> 
     <Byline> 
      <!-- check story --> 
      <xsl:choose> 
      <xsl:when test="contains(Body/div/p, '$$$')"> 
       <xsl:value-of select="substring-after(substring-before(Body/div/p, '$$$'), '£££')" /> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="CreatedBy"/> 
      </xsl:otherwise> 
      </xsl:choose> 
      </Byline> 
     <Images> 
      <xsl:for-each select="data"> 
      <xsl:element name="Image"> 
       <xsl:attribute name="id"> 
         <xsl:value-of select="@id"/> 
       </xsl:attribute> 
       <!--<xsl:attribute name="fileext"> 
         <xsl:value-of select="@ext"/> 
       </xsl:attribute>--> 
       <xsl:attribute name="filename"> 
       <xsl:value-of select="concat(@ImageToSaveID,'.jpg')"/> 
       </xsl:attribute> 
       <xsl:value-of select="urlpreview"/> 
      </xsl:element> 
      </xsl:for-each> 
     </Images> 
     </Article> 
    </xsl:for-each> 
</xsl:template> 




</xsl:stylesheet> 
+0

您的問題不清楚。請發佈您的預期結果,並解釋「*我失去了所有使用此方法的標籤*」的含義。 - 請記住,XSLT不會**作爲文本*處理您的輸入*;你不能將標籤連接到文本的開頭。 –

+0

嗨@ @ michael-hor257k我希望的輸出將如上面我目前得到的是故事文本文本文本等更多文本文本文本等沒有標籤謝謝安吉拉 –

+1

請不要在代碼中發表代碼 - 編輯您的問題,而不是。 –

回答

1

我不認爲你的問題是完全清楚,但讓你開始:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="p/text()[contains(., '$$$')]"> 
    <xsl:value-of select="substring-after(., '$$$')"/> 
</xsl:template> 

</xsl:stylesheet> 

適用於您例如輸入,結果會成爲:

<?xml version="1.0" encoding="UTF-8"?> 
<story> 
    <p>story text text text etc</p> 
    <p>more text text text etc</p> 
</story> 
+0

嗨,感謝您幫助邁克爾如何將此模板稱爲此模板..如果我將其添加到故事標籤中,則會出現錯誤。模板不能成爲故事的孩子 –

+0

@AngelaBaines你用什麼工具來做XSL轉換? –

+0

visual studio 2010 –