XSLT重新格式化XML文档
我希望制作一个XSLT来转换以下输入XML:XSLT重新格式化XML文档,xml,xslt,Xml,Xslt,我希望制作一个XSLT来转换以下输入XML: <?xml version="1.0" encoding="UTF-8"?> <payload> <header> <from>From: exmaple@domain.com</from> <to>To: exmaple@domain.com</to> </header> <email>
<?xml version="1.0" encoding="UTF-8"?>
<payload>
<header>
<from>From: exmaple@domain.com</from>
<to>To: exmaple@domain.com</to>
</header>
<email>
<body>Hello, email body here...</body>
</email>
<attachments>
<attachment>
<name>log1</name>
</attachment>
<attachment>
<name>log2</name>
</attachment>
</attachments>
</payload>
其中,在每次连接后重复“到”节点。可以有很多附件,而不仅仅是两个
来自用户的更新
我对此相当陌生,但我尝试创建一个没有标题、电子邮件和附件标签的有效负载。然后用来把它们排列成正确的顺序,但这看起来很可怕。而且不可扩展那么:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="from|to">
<xsl:value-of select="text()"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="body">
<xsl:text>
</xsl:text>
<xsl:value-of select="text()"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="attachments">
<xsl:for-each select="attachment">
<xsl:text>

</xsl:text>
<xsl:apply-templates select="../../header/to"/>
<xsl:value-of select="name/text()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
您尝试过什么?因此,这不是一个避免自己做任何繁重工作的地方。我对这一点很陌生,但我尝试创建一个没有标题
,电子邮件
,和附件
标签的有效负载。然后使用
将它们按正确的顺序排列,但这看起来很可怕。而且是不可伸缩的
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="from|to">
<xsl:value-of select="text()"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="body">
<xsl:text>
</xsl:text>
<xsl:value-of select="text()"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="attachments">
<xsl:for-each select="attachment">
<xsl:text>

</xsl:text>
<xsl:apply-templates select="../../header/to"/>
<xsl:value-of select="name/text()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>