2013-05-07 53 views
-1

我在下面提到了我的xml文件,xml包含許多內部樣式標記(請使用通用的xslt代碼來提取所有文本),但它應該提取所有文本在啓動innertag之前進行了內部和文本之後。如何從xml中使用xslt提取父標記的父標記文本和擴展文本

<?xml version="1.0" encoding="UTF-8"?> 
<Values> 
    <Value AttributeID="11218"> 
     <Text>WGP03068-CNZH-00 
       <style name="bold">Introduction of the Title</style>xslt 
       <style name="TextStyle1"> 
        The smallest font size for which kerning should be automatically adjusted. 
       </style> 
       <style name="bold">Reference for this book 
        <style name="TextStyle1"> 
        The smallest font size for which kerning should be automatically adjusted. 
       </style> 
       Hope you had a good learning experience. 
       </style> 
       I am expecting more solution for my doughts. 
      </Text> 
     </Value> 
    </Values> 

我的XSLT代碼如下提到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <HTML> 
      <xsl:apply-templates /> 
     </HTML> 
    </xsl:template> 

    <xsl:template match="Values"> 
     <xsl:for-each select="Value"> 
      <xsl:for-each select="Text"> 
       <p> 
       <xsl:for-each select="style"> 
        <xsl:value-of select="." /> 
       </xsl:for-each> 
       </p> 
      </xsl:for-each> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

我的XSLT缺少啓動文本和結束元素的文字它只讀取元素,我想全部外線和innertag文本閱讀,並添加自己的樣式(如粗體,斜體,字體名稱和顏色)

我期待輸出象下面這樣:

WGP03068-CNZH-00 標題介紹 xslt 應該自動調整字距的最小字體大小。 此書的參考資料 字距應該自動調整的最小字體大小,希望你有一個很好的學習經驗。 我期待爲我的麪糰提供更多解決方案。

回答

0

更好的方法是使用「推送」方法,並將XSLT樣式表編碼爲一系列匹配模板,並在其中輸出所需的html。

例如,要將XML中的文本元素轉換爲段落,可以使用以下模板。

<xsl:template match="Text"> 
    <p> 
     <xsl:apply-templates /> 
    </p> 
    </xsl:template> 

並輸出風格元素大膽,你會有這樣的一個模板:

<xsl:template match="style[@name='bold']"> 
    <span style="font-weight:bold"> 
     <xsl:apply-templates /> 
     </span> 
    </xsl:template> 

試試下面的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*" /> 

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

    <xsl:template match="Text"> 
    <p> 
     <xsl:apply-templates /> 
    </p> 
    </xsl:template> 

    <xsl:template match="style[@name='bold']"> 
    <span style="font-weight:bold"> 
     <xsl:apply-templates /> 
     </span> 
    </xsl:template> 

    <xsl:template match="style"> 
    <span> 
     <xsl:apply-templates /> 
    </span> 
    </xsl:template> 
</xsl:stylesheet> 

當適用於您的XML,以下是輸出

<HTML> 
    <p>WGP03068-CNZH-00 
     <span style="font-weight:bold">Introduction of the Title</span>xslt 
     <span> 
     The smallest font size for which kerning should be automatically adjusted. 
     </span><span style="font-weight:bold">Reference for this book 
     <span> 
      The smallest font size for which kerning should be automatically adjusted. 
      </span> 
     Hope you had a good learning experience. 
     </span> 
     I am expecting more solution for my doughts. 

    </p> 
</HTML> 

請注意,此XSLT使用XSLT的內置模板來輸出文本。在<xsl:apply-templates />和XSLT找到文本節點的位置,如果沒有匹配的模板,它會自動爲您輸出文本。

+0

這裏我使用ProcessingWordML將XML轉換爲Word文檔 – user2357252 2013-05-07 14:27:45

+0

如果XML具有 20不同的attributeID,如何設置Order格式。我必須檢查,如果具有AttributeID =「11111」它應該先顯示然後第二應該是AttributeID =「11218」等等..我需要檢查attributeID,然後放置位置。請任何人提供解決方案。 – user2357252 2013-05-16 11:54:59

+0

如果您爲此提出了一個全新的問題,而不是在評論中提問,那最好。我相信雖然解決這個問題並不難。 – 2013-05-16 12:26:11

相關問題