我有以下 XML 檔案:
<mail>
<body> Dear XY,
here are some random information as a placeholder.
==========================================
Fruit_Type: apple
Vagetable_Amount: potato
Animal_Counter: two dogs
==========================================
</body>
</mail>
我需要將正文字串縮短為這種形式:
<mail>
<body>
Fruit_Type: apple
Vagetable_Amount: potato
Animal_Counter: two dogs
</body>
</mail>
你有什么想法如何做到這一點?
uj5u.com熱心網友回復:
您可以嘗試以下方法:
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="body">
<xsl:copy>
<xsl:value-of select="substring-before(substring-after(., '===== '), ' =====')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
添加:
要按制表符縮進結果,您可以執行以下操作:
<xsl:template match="body">
<xsl:copy>
<xsl:for-each select="tokenize(substring-before(substring-after(., '===== '), ' ====='), ' ')">
<xsl:text> 	</xsl:text>
<xsl:value-of select="." />
</xsl:for-each>
</xsl:copy>
</xsl:template>
前提是您的處理器支持 XSLT 2.0。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494631.html
