使用我的代码,我们得到了一个不同的需求,我们必须将多个输入映射到一个无界元素。下面是给出的例子。
有人能帮个忙吗。
源系统XSD映射
<Errors>
<error>
<errorcode>code</errorcode>
<errortype>type</errortype>
<paramater1>error1</paramater1>
<paramater2>error2</paramater2>
<paramater3/>error3</paramater3>
<paramater4/>error4</paramater4>
<error>
<Errors>
与现在一样,我必须将这些值映射到目标xsd,它的格式如下
<Errors>
<error>
<errorcode>code</errorcode>
<errortype>type</errortype>
<errorParameters>
<errorPara>error1</errorPara>
<errorPara>error2</errorPara>
<errorPara>error3</errorPara>
<errorPara>error4</errorPara>
</errorParameters>
</error>
</Errors>
在这里,errorPara本质上是一个无界的,它位于errorParameters元素中。
我想知道如何使用XSLT1.0来实现这一点。
发布于 2016-03-23 06:18:21
好的,只有当错误只有错误代码、错误类型和parameterN的子错误时
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--Suppress unmatched text-->
<xsl:template match="text()" />
<xsl:template match="/">
<Errors><error>
<errorcode><xsl:apply-templates select="Errors/error/errorcode"/></errorcode>
<errortype><xsl:apply-templates select="Errors/error/errortype"/></errortype>
<errorParameters>
<xsl:for-each select="Errors/error/*">
<xsl:choose>
<xsl:when test="name() = 'errorcode'"></xsl:when>
<xsl:when test="name() = 'errortype'"></xsl:when>
<xsl:otherwise><errorPara><xsl:value-of select="normalize-space(.)"/></errorPara></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</errorParameters>
</error></Errors>
</xsl:template>
<xsl:template match="errorcode">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="errortype">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>
我认为这个问题上的数据xml有一些错误,所以我修改了它们:
<Errors>
<error>
<errorcode>code</errorcode>
<errortype>type</errortype>
<parameter1>error1</parameter1>
<parameter2>error2</parameter2>
<parameter3>error3</parameter3>
<parameter4>error4</parameter4>
</error>
</Errors>
然后是结果(Linux: xsltproc test.xslt data.xml \ xmllint -格式-):
<?xml version="1.0"?>
<Errors>
<error>
<errorcode>code</errorcode>
<errortype>type</errortype>
<errorParameters>
<errorPara>error1</errorPara>
<errorPara>error2</errorPara>
<errorPara>error3</errorPara>
<errorPara>error4</errorPara>
</errorParameters>
</error>
</Errors>
发布于 2016-03-23 09:16:30
这里有一种(简单的)方法来看待它:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="error">
<xsl:copy>
<xsl:copy-of select="errorcode | errortype"/>
<errorParameters>
<xsl:for-each select="*[starts-with(name(), 'paramater')]">
<errorPara>
<xsl:value-of select="."/>
</errorPara>
</xsl:for-each>
</errorParameters>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果除了errorcode
、errortype
和parameterN
之外,没有其他错误子级,则可以使用:
<xsl:for-each select="*[not(self::errorcode or self::errortype)]">
https://stackoverflow.com/questions/36169698
复制相似问题