如何在 XML 中拆分字符串 [英] How to split string in XML
问题描述
我有这种 XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="dataroot">
<xml><xsl:apply-templates/></xml>
</xsl:template>
<xsl:template match="M_17">
<package id="{package_id}" cat="{cat}">
<nazwa><xsl:value-of select="nazwa"/></nazwa>
<xsl:if test="author"><author><xsl:value-of select="author"/></author></xsl:if>
<xsl:if test="www"><www><xsl:value-of select="translate(www,'#','')"/></www></xsl:if>
<xsl:if test="opis"><opis><xsl:value-of select="opis"/></opis></xsl:if>
<xsl:if test="img"><img><xsl:value-of select="translate(img,'#','')"/></img></xsl:if>
<xsl:if test="depends"><depends><xsl:value-of select="depends"/></depends></xsl:if>
<xsl:if test="conflicts"><conflicts><xsl:value-of select="conflicts"/></conflicts></xsl:if>
<xsl:if test="after"><after><xsl:value-of select="after"/></after></xsl:if>
<xsl:if test="replaces"><replaces><xsl:value-of select="replaces"/></replaces></xsl:if>
</package>
</xsl:template>
</xsl:stylesheet>
但是当有在例如.取决于此代码中将显示的 2 个值
but when there is in eg. depends 2 values which in this code will show
<depends>modload com1node</depends>
但我想通过以下 XSL 将其转换为:
but I want to convert it by followed XSL to:
<depends>modloader</depends>
<depends>com1node</depends>
这应该发生在:依赖、冲突、之后和替换
This should happened for: depends, conflicts, after and replaces
如何将这些字符串(如果它们出现在源 XML 中)拆分为简单的字符串(如我在示例中所示,每行一个)?
How to split this strings (if they occur in source XML) into simple ones (one in each line as I show in example)?
核心 XML 的一部分
Part of Core XML
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2014-05-11T15:51:32">
<Mnc_172>
<ID>1</ID>
<package_id>minecraft</package_id>
<cat>lib</cat>
<www>#http://minecraft.net/#</www>
<nazwa>Minecraft</nazwa>
<author>Mojang</author>
<opis>Game - build your own world!</opis>
<img>#/mc.png#</img>
</Mnc_172>
<Mnc_172>
<ID>2</ID>
<package_id>modloader</package_id>
<cat>lib</cat>
<www>#http://minecraftforum.net/topic/75440-x/#</www>
<nazwa>ModLoader</nazwa>
<author>Risugami</author>
<opis>ModLoader - library to load mods</opis>
<img>#/gen.png#</img>
<replaces>modL forging</replaces>
</Mnc_172>
...
</dataroot>
推荐答案
XML 与您的 XSLT 不匹配:M_17
vs. Mnc_172
.无论如何,在 XSLT 1.0 中,您需要使用递归模板来标记内容.所以尝试改变:
The XML does not match your XSLT: M_17
vs. Mnc_172
. Anyway, in XSLT 1.0 you need to use a recursive template to tokenize the contents. So try changing:
<depends><xsl:value-of select="depends"/></depends>
到:
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="depends"/>
<xsl:with-param name="elemName" select="'depends'"/>
</xsl:call-template>
并将以下模板添加到您的样式表中:
and add the following template to your stylesheet:
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="elemName"/>
<xsl:param name="sep" select="' '"/>
<xsl:choose>
<xsl:when test="contains($text, $sep)">
<xsl:element name="{$elemName}">
<xsl:value-of select="substring-before($text, $sep)"/>
</xsl:element>
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $sep)" />
<xsl:with-param name="elemName" select="$elemName" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$elemName}">
<xsl:value-of select="$text"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
这篇关于如何在 XML 中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!