运行以下输入XML时
<root>
<value>false</value>
<value>true</value>
</root>
Run Code Online (Sandbox Code Playgroud)
针对以下XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="value">
<true_value/>
</xsl:template>
<xsl:template match="value[. = 'false']">
<false_value/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
我的value元素带有'false',因为它的内容改为false_value..而且所有其他value元素都变成了true_value.输出:
<?xml version="1.0" encoding="utf-8"?>
<root>
<false_value/>
<true_value/>
</root>
Run Code Online (Sandbox Code Playgroud)
但是只有当我更改模板匹配时才会出现root/value模糊的模板警告.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root/value">
<true_value/>
</xsl:template>
<xsl:template match="root/value[. = 'false']">
<false_value/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
请帮助我解释root在 …
这个问题与michael.hor257k 最近的答案 有关,后者与Dimitre Novatchev 的答案有关.
当在上面提到的答案中使用样式表(由michael.hor257k)时,对于大型XML(大约60MB,下面是样本XML),转换成功执行.
当尝试另一个样式表,与michael.hor257k有点不同,并且旨在将元素(带有子sectPr)和它们的后续兄弟(直到下一个带有子元素的下一个兄弟元素sectPr)分组,递归(即,将元素分组到输入XML的深度).
示例输入XML:
<body>
<p/>
<p>
<sectPr/>
</p>
<p/>
<p/>
<tbl/>
<p>
<sectPr/>
</p>
<p/>
</body>
Run Code Online (Sandbox Code Playgroud)
我试过的样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="*[1] | *[sectPr]"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::*[1][not(sectPr)]"/>
</xsl:template>
<xsl:template match="*[sectPr]">
<myTag>
<xsl:copy>
<xsl:apply-templates select="*[1] | *[sectPr]"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::*[1][not(sectPr)]"/>
</myTag>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
出于好奇,我遇到了OutOfMemoryError转换大约60MB的XML.
我想知道,我认为我不理解michael.hor257k和Dimitre Novatchev提供的XSLT背后的技巧,它不会导致内存异常.
我得到OutOfMemoryError的样式表和上面提到的答案之间有什么大的区别.如何更新样式表以提高内存效率.