使用xslt从xml中删除换行符
问题1: 我有:使用xslt从xml中删除换行符,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,问题1: 我有: <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 托弗 贾尼 提醒 这个周末别忘了我! 我想要的是(在一行中): ToveJaniReminderDon这个周末别忘了我!
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
托弗
贾尼
提醒
这个周末别忘了我!
我想要的是(在一行中):
ToveJaniReminderDon这个周末别忘了我!
问题2
要为每个
<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>
<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>
托弗
贾尼
提醒
这个周末别忘了我!
托弗
贾尼
提醒
这个周末别忘了我!
所需输出应为(2行2条记录:
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
ToveJaniReminderDon这个周末别忘了我!
这个周末别忘了我!
请帮助我实现这一点。如果需要,很高兴提供投入
提前感谢!
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>
You may use this code for single line
您可以将此代码用于单行
通过添加根元素,您可以使用
请注意,您的请求可能表明存在某些设计问题,因为XML文档之间的序列化差异从未考虑过。此外,XSLT处理器可能不负责序列化
此格式良好的输入:
<root>
<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>
<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>
</root>
托弗
贾尼
提醒
这个周末别忘了我!
托弗
贾尼
提醒
这个周末别忘了我!
使用此样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="TEST|/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="note">
<xsl:call-template name="identity"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
;
输出:
ToveJaniReminderDon这个周末别忘了我!
这个周末别忘了我!
;
对于第二个问题,您的xml格式不好谢谢@imran,在添加了省略xml声明的行之后,它给出了所需的输出。非常感谢@Alejandro!它对我很好。您能告诉我应该如何学习这些东西吗?我也不能理解您的解决方案,因为我是xml和xslt新手。再次感谢。谢谢lot@sandeep kamboj!这对我来说很好。你能告诉我我应该如何学习这些东西吗?我也不能理解你的解决方案,因为我是xml和xslt新手。再次感谢。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="TEST|/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="note">
<xsl:call-template name="identity"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" indent="no" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()">
<xsl:apply-templates select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="TEST">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="note">
<xsl:text>
</xsl:text>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>