XML:
<?xml version="1.0" encoding="UTF-8"?>
<employee>
<name id="8011810" loc="CHN" act="TVN">Ram
<Prev_name pid="789546" ploc="TN" pact="VRT">Kumar</Prev_name>
</name>
<project ppid="8011475" pploc="HYD" ppact="BT">ODC</project>
<team tid="456987" loc="BAN" Act="SCP" size="small">CMS</team>
</employee>
XSLT:
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match="employee">
<xsl:for-each select="@*|node()">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
预期产出:
8011810CHNTVNRam789546TNVRTKumar8011475HYDBTODC456987BANSCPsmallCMS
注意:必须读取所有子节点,包括其属性。子节点node_names可以是任何东西。
结束输出应该是<employee>
标记
答案 0 :(得分:0)
默认情况下,属性会被忽略,因此您需要一个模板来复制它们。试试这个:
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:apply-templates select="@* | node()"/>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
要获得employee
下的所有内容,而不是其他标记/文本节点,我选择了一种模板上具有模式属性的方法。具有mode-attribute的模板只有在apply-templates
中使用相同的mode-attribute调用时才匹配。我在文本节点上使用normalize-space()
来摆脱换行符和空格。
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match="employee">
<xsl:apply-templates select="@* | node()" mode="only" /> <!-- apply identity template to employee children only -->
</xsl:template>
<xsl:template match="node()" mode="only"> <!-- only for employee children -->
<xsl:apply-templates select="@* | node()" mode="only" />
</xsl:template>
<xsl:template match="text()" mode="only"> <!-- text()-nodes only for employee children -->
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
<xsl:template match="text()" /> <!-- suppress all other text -->
</xsl:stylesheet>