为什么文本节点会出现在转换后的 xml 中 [英] Why do text nodes appear in transformed xml

查看:25
本文介绍了为什么文本节点会出现在转换后的 xml 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 xslt 从 xml 文件中的元素中选择一些具有特定值的节点集.我确实得到了我想要的节点,但我也从 textnodes 得到了一些序列化的文本.你能帮我把这段文字去掉吗?

I want to use xslt to select some node-sets with a specific value in an element from an xml-file. I do get the node I want, but I also get some the serialized text from textnodes. Can you please help me to get rid of this text?

这是源文件:

<surveys>
<survey id='01'>
    <category>cat1</category>
    <questions>
        <question id='1'>Y</question>
        <question id='2'>Y</question>
        <question id='3'>Y</question>
        <question id='4'>Y</question>
    </questions>
</survey>
<survey id='02'>
    <category>cat2</category>
    <questions>
        <question id='1'>Y</question>
        <question id='2'>Y</question>
        <question id='3'>N</question>
        <question id='4'>N</question>
    </questions>
</survey>
<survey id='03'>
    <category>cat1</category>
    <questions>
        <question id='1'>N</question>
        <question id='2'>N</question>
        <question id='3'>N</question>
        <question id='4'>N</question>
    </questions>
</survey>
<survey id='04'>
    <category>cat3</category>
    <questions>
        <question id='1'>N</question>
        <question id='2'>N</question>
        <question id='3'>Y</question>
        <question id='4'>Y</question>
    </questions>
</survey>
</surveys>

这是转换文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
    <surveys>
        <category/>
        <xsl:apply-templates/>
    </surveys>
</xsl:template>

<xsl:template match="survey[category = 'cat2']">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

结果如下:

<surveys>cat1YYYY<survey id="02">
    <category>cat2</category>
    <questions>
        <question id="1">Y</question>
        <question id="2">Y</question>
        <question id="3">N</question>
        <question id="4">N</question>
    </questions>
</survey>cat1NNNNcat3NNYY</surveys>

所以,我想在调查元素之后的第一行中去掉cat1YYYY",在调查元素之后的最后一行中去掉cat1NNNNcat3NNYY".我想知道它为什么在那里 ;-)

So, I'd like to get rid of "cat1YYYY" in the first line after the surveys element and of "cat1NNNNcat3NNYY" in the last line after the survey element. And I'd like to understand why it's there ;-)

推荐答案

我想知道它为什么在那里

I'd like to understand why it's there

它的存在是因为您不加选择地应用模板 - 而 XSLT 有一些 内置-在模板规则中将文本节点复制为默认值.

It's there because you are applying templates indiscriminately - and XSLT has some built-in template rules that copy text nodes as the default.

为了防止这种情况发生,您可以添加自己的模板来覆盖默认行为:

To prevent this from happening, you could add your own template to override the default behavior:

<xsl:template match="text()" />

或者 - 最好 - 有选择地应用模板开始:

or - preferably - apply templates selectively to begin with:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/surveys">
    <surveys>
        <category/>
        <xsl:apply-templates select="survey[category = 'cat2']"/>
    </surveys>
</xsl:template>

<xsl:template match="survey">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

BTW 可以缩写为:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/surveys">
    <surveys>
        <category/>
        <xsl:copy-of select="survey[category = 'cat2']"/>
    </surveys>
</xsl:template>

</xsl:stylesheet>

这篇关于为什么文本节点会出现在转换后的 xml 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆