我的文件如下所示
<list>
<document>
<content type="text" name="title" action="cluster" weight="3" output-action="bold" vse-streams="2" u="113">
Sample action steps/best practices</content>
<content type="html" name="title" output-action="bold" weight="3" vse-streams="2" u="146">
Survey Dimension: Excellence in All We Do</content>
<content name="fileextension" action="none" weight="1" type="text" output-action="bold" vse-streams="2" u="43">
pdf</content>
<content name="filetypelabel" action="none" weight="1" type="text" output-action="" vse-streams="2" u="45">
word</content>
</document>
<document>
<content type="text" name="title" action="cluster" weight="3" output-action="bold" vse-streams="2" u="113">
Sample steps/best practices</content>
<content type="html" name="title" output-action="bold" weight="3" vse-streams="2" u="146">
Survey : Excellence in All We Do</content>
<content name="fileextension" action="none" weight="1" type="text" output-action="bold" vse-streams="2" u="43">
excel</content>
<content name="filetypelabel" action="none" weight="1" type="text" output-action="" vse-streams="2" u="45">
ppt</content>
</document>
</list>
当我使用xslt 2.0
进行转换时 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:template match="list/document">
<xsl:for-each select ="content">
<xsl:variable name="title" select="." />
<xsl:value-of select="$title" />
</xsl:for-each >
</xsl:template>
</xsl:stylesheet>
我得到了如下
<?xml version="1.0" encoding="utf-8"?>
Sample action steps/best practices
Survey Dimension: Excellence in All We Do
pdf
word
Sample steps/best practices
Survey : Excellence in All We Do
excel
ppt
但我的要求是在两个文件中考虑@ name ='title'时的第一个元素值。
所以预期结果应如下所示
<?xml version="1.0" encoding="utf-8"?>
Sample action steps/best practices
Sample steps/best practices
答案 0 :(得分:0)
首先,XSLT样式表中有几个未使用的名称空间:
xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
从代码中删除这种混乱。然后,对于像使用元素值一样简单的事情,不需要将它存储在xsl:variable
元素中,只需直接使用xsl:value-of
:
<xsl:value-of select="."/>
但我的要求是在两个文件中的@ name ='title'时考虑第一个元素值
只需将这些规则放入谓词:
content[@name = 'title' and position() = 1]
XSLT样式表
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" encoding="UTF-8" />
<xsl:template match="content[@name = 'title' and position() = 1]">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:transform>
文字输出
[Empty line here]
Sample action steps/best practices
Sample steps/best practices
如果空格控制对您很重要,您可能更喜欢
之类的东西<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" encoding="UTF-8" />
<xsl:strip-space elements="*"/>
<xsl:template match="/list">
<xsl:for-each select="document/content[@name = 'title' and position() = 1]">
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>
</xsl:transform>