[英]Retrieving text from XML node with XSLT
如果這看起來像一個非常基本的問題,我很抱歉,但我真的(我的意思是真的)感謝我能得到的任何幫助。 我只是想做以下事情:
1.更換自動關閉
2.從'Second_node'抓取文本
3.將該文本存儲在變量中
4.將該文本放入新的“Seventh_node”中。
我已經完成了第1步,但我似乎無法從所需元素中檢索必要的信息。 我在下面列舉了三個例子以及我正在使用的XSLT。 我想關鍵問題是存儲'Second_node'的文本內容並將其放在新元素中。 通過添加信息,我使用Saxon 6.5進行轉換。 如果提供的信息仍然不完整,請告訴我。
謝謝!
源XML:
<firstnode>
<Second_node>text for second node</Second_node>
<Third_node>
<Fourth_node>
<Fifth_node>text for fifth node</Fifth_node>
<Sixth_node>text for sixth node</Sixth_node>
<Seventh_node />
</Fourth_node>
</Third_node>
</firstnode>
到目前為止我所擁有的:
<firstnode>
<Second_node>text for second node</Second_node>
<Third_node>
<Fourth_node>
<Fifth_node>text for fifth node</Fifth_node>
<Sixth_node>text for sixth node</Sixth_node>
<Seventh_node></Seventh_node>
</Fourth_node>
</Third_node>
</firstnode>
我需要的:
<firstnode>
<Second_node>text for second node</Second_node>
<Third_node>
<Fourth_node>
<Fifth_node>text for fifth node</Fifth_node>
<Sixth_node>text for sixth node</Sixth_node>
<Seventh_node>text for second node</Seventh_node>
</Fourth_node>
</Third_node>
</firstnode>
到目前為止我的XSLT:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Seventh_node">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:text>text for second node</xsl:text>
</xsl:copy>
</xsl:template>
您可以嘗試將第二個節點中的文本放入變量中,然后使用該變量將該文本放在第七個節點中。
<xsl:variable name="VAR_SecNode">
<xsl:value-of select="Second_node"/>
</xsl:variable>
...
<Seventh_node><xsl:value-of select="$VAR_SecNode" /></Seventh_node>
...
我認為不需要變量。
嘗試這個:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Seventh_node">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="//Second_node"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:apply-templates select="@*"/>
將復制Seventh_node的屬性。 <xsl:apply-templates select="node()"/>
將復制Seventh_node的子節點。 如果你不需要刪除這一行。
您可以將第二個模板更新為以下內容:
<xsl:template match="Seventh_node/text()">
<xsl:text>text for second node</xsl:text>
</xsl:template>
這將僅匹配您的Seventh_node
的文本,並將其替換為您放在<xsl:text>
元素中的任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.