我试图使用XSLT对某些XML进行转换。
我的xml是;
<body>
<p>
<a href="http://www.zz.com/abc/z/0/z3970z88-0475-11dz-8603-00144zeabdc1.html#slide0"></a>
Some <strong>strong</strong> text
</p>
</body>我想把它变成;
<body>
<slideshow data-uuid="z3970z88-0475-11dz-8603-00144zeabdc1/>
<p>Some <strong>strong</strong> text</p>
</body>到目前为止,我所拥有的是;
<xsl:template match="/body/p[a[substring(@href, string-length(@href) - 6) = '#slide0' and string-length(text()) = 0] and count(*) = 1]">
<xsl:apply-templates select="a" />
<xsl:if test="string-length(text()) > 0">
<p>
<xsl:value-of select="text()"/>
</p>
</xsl:if>
</xsl:template>
<xsl:template match="a[substring(@href, string-length(@href) - 6) = '#slide0' and string-length(text()) = 0]">
<slideshow>
<xsl:attribute name="data-uuid">
<xsl:value-of select="substring-before(substring(@href, string-length(@href) - 47), '.html#slide0')" />
</xsl:attribute>
</xsl:template>但是,只有在文本没有任何子标记(如<strong>标记或另一个<a>标记)的情况下,这才有效。
有没有人有办法把这一切都拿出来。
发布于 2015-07-13 13:53:34
我已经想出了这个XSLT。我不确定是否适合你想出的每一个目的,但它确实解决了你在这里给出的例子:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/body/p[a]">
<xsl:apply-templates select="a" />
<p>
<xsl:copy-of select="./node()[not(self::a)]" />
</p>
</xsl:template>
<xsl:template match="a[substring(@href, string-length(@href) - 6) = '#slide0' and string-length(text()) = 0]">
<slideshow>
<xsl:attribute name="data-uuid">
<xsl:value-of select="substring-before(substring(@href, string-length(@href) - 47), '.html#slide0')" />
</xsl:attribute>
</slideshow>
</xsl:template>
</xsl:stylesheet>这样做的目的是复制<p>标记的内容,期望使用<a>标记。我更改了第一个应用模板的条件,但您不必这样做。这只会让我更容易读懂。
发布于 2015-07-13 20:21:22
在只转换部分XML的情况下,从身份模板开始通常是一种很好的方法。
<xsl:template match="@*|node()" >
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>这意味着,与其在模板中使用xsl:value-of,不如使用xsl:apply-templates并让标识templ
<xsl:if test="string-length(text()) > 0">
<p>
<xsl:apply-templates select="@*|node()[not(self::a)]"/>
</p>
</xsl:if>试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="p[a[substring(@href, string-length(@href) - 6) = '#slide0' and string-length(text()) = 0] and count(*) = 1]">
<xsl:apply-templates select="a" mode="slideshow" />
<xsl:if test="string-length(text()) > 0">
<p>
<xsl:apply-templates select="@*|node()[not(self::a)]"/>
</p>
</xsl:if>
</xsl:template>
<xsl:template match="a" mode="slideshow">
<slideshow>
<xsl:attribute name="data-uuid">
<xsl:value-of select="substring-before(substring(@href, string-length(@href) - 47), '.html#slide0')" />
</xsl:attribute>
</slideshow>
</xsl:template>
<xsl:template match="@*|node()" >
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>注意,在匹配a的模板中使用了“模式”。这纯粹是为了避免对长卷的情况编码两次。
顺便说一句,在属性值模板模板中使用a来简化它
<xsl:template match="a" mode="slideshow">
<slideshow data-uuid="{substring-before(substring(@href, string-length(@href) - 47), '.html#slide0')}" />
</xsl:template>https://stackoverflow.com/questions/31384448
复制相似问题