我是XSLT的新手,有问题为下面的示例xml编写转换。
示例XML
<exports>
<job>Job1</job>
<date>Wed Sep 27 13:19:29 CDT 2017</date>
<targets>
<target server-name="">
<templates>
<template />
<template />
</templates>
<components>
<component>test2server</component>
<component>test4server</component>
</components>
</target>
</targets>
<auditrun template="RHEL">
<objects-audited />
<Changed-targets>
<target>test2server</target>
<target>test4server</target>
</Changed-targets>
<unchanged-targets />
<changed-audit-items>
<node1>"file"</node1>
<node2>"filename1"</node2>
<node3>"filename1"</node3>
<target>"test2server"</target>
<node5>"lastnight"</node5>
</changed-audit-items>
<changed-audit-items>
<node1>"registryval"</node1>
<node2>"filename2"</node2>
<node3>"filename2"</node3>
<target>"test4server"</target>
<node5>"morning"</node5>
</changed-audit-items>
</auditrun>
</exports>
所需输出
Changed Audit Items 1
node 1 - file
node 2 - filename1
node 3 - filename1
target - test2server
node 5 - lastnight
Changed Audit items 2
node 1 - registryval
node 2 - filename2
node 3 - filename2
target - test4server
node 5 - morning
我的转型 - 我正在使用以下for循环并添加了文字&amp;值打印输出但未能获得所需的输出。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="text" indent="no" />
<xsl:template match="/exports">
<xsl:apply-templates select="auditrun" />
</xsl:template>
<xsl:template match="auditrun/changed-audit-items">
<xsl:for-each select="exports/auditrun/changed-audit-items/node3">
<xsl:text disable-output-escaping="no"> </xsl:text>
<xsl:text disable-output-escaping="yes"> Changed Targets: </xsl:text>
<xsl:value-of select="//changed-audit-items/target/text()" disable-output-escaping="no" />
</xsl:for-each>
<xsl:variable select="1" name="counter" />
<xsl:variable select="count(//changed-audit-items/node3)" name="CountAuditItems" />
<Audit>
<xsl:value-of select="count(//Changed-targets/target)" disable-output-escaping="no" />
<xsl:text disable-output-escaping="no"> Total number of Changed Server Items: </xsl:text>
<xsl:value-of select="count(//changed-audit-items/node3)" disable-output-escaping="no" />
</Audit>
<xsl:text disable-output-escaping="no"> </xsl:text>
<xsl:text disable-output-escaping="no">node1 </xsl:text>
<xsl:value-of select="//changed-audit-items/node1/text()" disable-output-escaping="yes" />
<xsl:text disable-output-escaping="no"> </xsl:text>
<xsl:text disable-output-escaping="no">node3: </xsl:text>
<xsl:value-of select="//changed-audit-items/node3/text()" disable-output-escaping="no" />
<xsl:text disable-output-escaping="no"> </xsl:text>
<xsl:text disable-output-escaping="no">Node2: </xsl:text>
<xsl:value-of select="//changed-audit-items/node2/text()" disable-output-escaping="no" />
<xsl:text disable-output-escaping="no"> </xsl:text>
<xsl:text disable-output-escaping="no">node4</xsl:text>
<xsl:value-of select="//changed-audit-items/node4/text()" disable-output-escaping="no" />
<xsl:text disable-output-escaping="no"> </xsl:text>
</xsl:template>
</xsl:stylesheet>
我想请专家指导。
此致 [R
答案 0 :(得分:0)
使用嵌套的<xsl:for-each>
循环可以实现所需的输出。外部用于循环<changed-audit-items>
节点以添加标题Changed Audit Items
,内部用于循环<changed-audit-items>
的所有子节点。内部循环将在输出中打印节点名称及其值。
以下是XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="groupTitle" select="'Changed Audit Items '" />
<xsl:variable name="newline" select="' '" />
<xsl:variable name="hyphenSeparator" select="' - '" />
<xsl:template match="/">
<!-- loop for all <changed-audit-items> nodes -->
<xsl:for-each select="//changed-audit-items">
<xsl:value-of select="concat($groupTitle, position())" />
<xsl:value-of select="$newline" />
<!-- loop for all child nodes of <changed-audit-items> -->
<xsl:for-each select="./*">
<xsl:value-of select="local-name(.)" />
<xsl:value-of select="$hyphenSeparator" />
<xsl:value-of select="." disable-output-escaping="no"/>
<xsl:value-of select="$newline" />
</xsl:for-each>
<xsl:value-of select="$newline" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出
Changed Audit Items 1
node1 - "file"
node2 - "filename1"
node3 - "filename1"
target - "test2server"
node5 - "lastnight"
Changed Audit Items 2
node1 - "registryval"
node2 - "filename2"
node3 - "filename2"
target - "test4server"
node5 - "morning"