XSLT 过滤器 XML 除了某些元素
XSLT filter XML except certen elements
我想过滤 XML。
我得到的那些很大,对我来说有很多无用的信息。
我只需要一些包含此文件中文本的元素。
这几乎就是它的样子。
<root>
<customerinfo1>...</customerinfo1>
<customerinfo2>...</customerinfo2>
<productinfo>
<productinfo1>...</productinfo1>
<productinfo2...></productinfo2>
<textarea>
<other1>...</other1>
<other2>...</other2>
<text1>abc</text1>
<text2>cab</text2>
<text3>bca</text3>
<other3>...</other3>
</textarea>
</productinfo>
</root>
这几乎就是它的样子。并非所有元素都有文本。我想将其中带有文本的元素添加到一个元素中。
我想要的是类似这样的东西。
<placement>
<text>Text from text1 text2 or text3</text>
</placement>
我做了什么 XSLT 以及它做了什么。
所以我设法将它排序为文本元素,而不是将它们分组为一个元素。
最大的问题是所有文本都从 <text>No text here</text>
中消失,而我在 XML 中得到的元素从一开始就为空。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="*[starts-with(name(),'text')]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<placement>
<text>
<xsl:apply-templates/>
</text>
</placement>
</xsl:template>
</xsl:stylesheet>
I want the textarea to become placement and the text1, text2, text3 to
become one <text>Text from text1, 2 and 3 here </text>
怎么样:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<placement>
<text>
<xsl:value-of select="//textarea/*[starts-with(name(),'text')]"/>
</text>
</placement>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例(更正后<productinfo2..>
),结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<placement>
<text>abc cab bca</text>
</placement>
谢谢。稍加编辑就可以很好地工作。
必须将 select 标签更改为
<xsl:apply-templates select="//*[starts-with(name(),'text')]"/>
文本元素的顺序不正确,更像是这样
<other1>...</other1>
<text1>abc</text1>
<other2>...</other2>
<text2>cab</text2>
<text3>bca</text3>
<other3>...</other3>
我想过滤 XML。 我得到的那些很大,对我来说有很多无用的信息。
我只需要一些包含此文件中文本的元素。 这几乎就是它的样子。
<root>
<customerinfo1>...</customerinfo1>
<customerinfo2>...</customerinfo2>
<productinfo>
<productinfo1>...</productinfo1>
<productinfo2...></productinfo2>
<textarea>
<other1>...</other1>
<other2>...</other2>
<text1>abc</text1>
<text2>cab</text2>
<text3>bca</text3>
<other3>...</other3>
</textarea>
</productinfo>
</root>
这几乎就是它的样子。并非所有元素都有文本。我想将其中带有文本的元素添加到一个元素中。 我想要的是类似这样的东西。
<placement>
<text>Text from text1 text2 or text3</text>
</placement>
我做了什么 XSLT 以及它做了什么。
所以我设法将它排序为文本元素,而不是将它们分组为一个元素。
最大的问题是所有文本都从 <text>No text here</text>
中消失,而我在 XML 中得到的元素从一开始就为空。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="*[starts-with(name(),'text')]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<placement>
<text>
<xsl:apply-templates/>
</text>
</placement>
</xsl:template>
</xsl:stylesheet>
I want the textarea to become placement and the text1, text2, text3 to become one
<text>Text from text1, 2 and 3 here </text>
怎么样:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<placement>
<text>
<xsl:value-of select="//textarea/*[starts-with(name(),'text')]"/>
</text>
</placement>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例(更正后<productinfo2..>
),结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<placement>
<text>abc cab bca</text>
</placement>
谢谢。稍加编辑就可以很好地工作。 必须将 select 标签更改为
<xsl:apply-templates select="//*[starts-with(name(),'text')]"/>
文本元素的顺序不正确,更像是这样
<other1>...</other1>
<text1>abc</text1>
<other2>...</other2>
<text2>cab</text2>
<text3>bca</text3>
<other3>...</other3>