尝试使用一个默认值并跳过元素将 xml 转换为 xslt
Trying to convert xml to xslt with one default and skipping elements
我查看了此处的示例以了解如何执行此操作,但它似乎对我不起作用。我正在使用 Stylus Studio 2011 编写和测试我的结果,我的要求是将 xml 文件转换为固定宽度。
这是来源:
> <?xml version="1.0" encoding="UTF-8"?> <PublishNASAMILE
> xmlns="http://www.ibm.com/maximo"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> creationDateTime="2015-06-09T13:48:33-05:00" transLanguage="EN"
> baseLanguage="EN" messageID="1433875714494891022" maximoVersion="7 5
> 20130829-1209 V7510--1" event="0"> <NASAMILESet>
> <ASSET>
> <ASSETNUM>G10-1313K</ASSETNUM>
> <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
> <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
> <NASALICENSE>G101313K</NASALICENSE>
> <NASAODOMETER>73660</NASAODOMETER>
> <SITEID>MP</SITEID>
> </ASSET>
> <ASSET>
> <ASSETNUM>G10-2465M</ASSETNUM>
> <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
> <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
> <NASALICENSE>G102465M</NASALICENSE>
> <NASAODOMETER>108590</NASAODOMETER>
> <SITEID>MP</SITEID>
> </ASSET>
> <ASSET>
> <ASSETNUM>G10-2469M</ASSETNUM>
> <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
> <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
> <NASALICENSE>G102469M</NASALICENSE>
> <NASAODOMETER>78999</NASAODOMETER>
> <SITEID>MP</SITEID>
> </ASSET> </NASAMILESet> </PublishNASAMILE>
期望的输出
32M G10 G101313K 73660
32M G10 G102465M 108590
32M G10 g102469M 78999
第一个值是默认值,标签不存在于 XML
间距为:
32M 1-25
G10 26-29
标记 30-36
37-42 英里
我是根据网站上的示例完成的
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" encoding="ASCII" method="text"/>
<xsl:strip-space elements=" "/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ASSET">
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:template>
<xsl:variable name = "TYPE" select = "'32M'" />
<xsl:template match="TYPE">
<xsl:value-of
select="substring(concat(., ' '), 1, 25)"/>
</xsl:template>
<xsl:template match="NASAGSAVHCLCLASS">
<xsl:value-of
select="substring(concat(., ' '), 1, 4)"/>
</xsl:template>
<xsl:template match="NASALICENSE">
<xsl:value-of
select="substring(concat(., ' '), 1, 7)"/>
</xsl:template>
<xsl:template match="NASAODOMETER">
<xsl:value-of
select="substring(concat(., ' '), 1, 6)"/>
</xsl:template>
<xsl:template match="SITEID"/>
</xsl:stylesheet>
这是我从这个文件中得到的输出
{
G10-1313K
GSA
G10
G101313K
73660
MP
G10-2465M
GSA
G10
G102465M
108590
MP
G10-2469M
GSA
G10
G102469M
78999
MP
}
似乎不起作用的是每条记录都不在一行上,我想跳过 xml 的前两个节点并从默认值 32M 开始,并且还想跳过最后一个元素 "MP".
在我的尝试中,我尝试了匹配,但没有指定任何内容来跳过我不想要的节点,我尝试创建一个变量并与之匹配,但输出似乎没有任何变化。
我不太精通 xslt,所以非常感谢帮助。
第一件事:如果你想要固定宽度的文本输出,那么不要使用恒等变换模板——那是没有意义的。
第二件事:输入的节点 XML 在命名空间中;因此,您的模板不会 select 任何东西,因为它们在无名称空间中寻址节点。你看到的结果完全是由 built-in template rules 产生的。使用完全没有模板的样式表会得到相同的结果。
现在,我无法理解您对所需输出的描述:您提供的数字与您显示的预期结果不符。试试这个作为你的起点:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mx="http://www.ibm.com/maximo">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="spaces" select="' '"/>
<xsl:template match="/mx:PublishNASAMILE">
<xsl:for-each select="mx:NASAMILESet/mx:ASSET">
<xsl:text>32M </xsl:text>
<xsl:value-of select="substring(concat(mx:NASAGSAVHCLCLASS, $spaces), 1, 4)"/>
<xsl:value-of select="substring(concat(mx:NASALICENSE, $spaces), 1, 10)"/>
<xsl:value-of select="substring(concat(mx:NASAODOMETER, $spaces), 1, 6)"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
请注意命名空间声明以及对输入文档中 select 个节点的关联前缀的使用。
应用于您的输入示例,结果将是:
32M G10 G101313K 73660
32M G10 G102465M 108590
32M G10 G102469M 78999
我查看了此处的示例以了解如何执行此操作,但它似乎对我不起作用。我正在使用 Stylus Studio 2011 编写和测试我的结果,我的要求是将 xml 文件转换为固定宽度。
这是来源:
> <?xml version="1.0" encoding="UTF-8"?> <PublishNASAMILE
> xmlns="http://www.ibm.com/maximo"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> creationDateTime="2015-06-09T13:48:33-05:00" transLanguage="EN"
> baseLanguage="EN" messageID="1433875714494891022" maximoVersion="7 5
> 20130829-1209 V7510--1" event="0"> <NASAMILESet>
> <ASSET>
> <ASSETNUM>G10-1313K</ASSETNUM>
> <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
> <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
> <NASALICENSE>G101313K</NASALICENSE>
> <NASAODOMETER>73660</NASAODOMETER>
> <SITEID>MP</SITEID>
> </ASSET>
> <ASSET>
> <ASSETNUM>G10-2465M</ASSETNUM>
> <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
> <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
> <NASALICENSE>G102465M</NASALICENSE>
> <NASAODOMETER>108590</NASAODOMETER>
> <SITEID>MP</SITEID>
> </ASSET>
> <ASSET>
> <ASSETNUM>G10-2469M</ASSETNUM>
> <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
> <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
> <NASALICENSE>G102469M</NASALICENSE>
> <NASAODOMETER>78999</NASAODOMETER>
> <SITEID>MP</SITEID>
> </ASSET> </NASAMILESet> </PublishNASAMILE>
期望的输出
32M G10 G101313K 73660
32M G10 G102465M 108590
32M G10 g102469M 78999
第一个值是默认值,标签不存在于 XML 间距为: 32M 1-25 G10 26-29 标记 30-36 37-42 英里
我是根据网站上的示例完成的
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" encoding="ASCII" method="text"/>
<xsl:strip-space elements=" "/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ASSET">
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:template>
<xsl:variable name = "TYPE" select = "'32M'" />
<xsl:template match="TYPE">
<xsl:value-of
select="substring(concat(., ' '), 1, 25)"/>
</xsl:template>
<xsl:template match="NASAGSAVHCLCLASS">
<xsl:value-of
select="substring(concat(., ' '), 1, 4)"/>
</xsl:template>
<xsl:template match="NASALICENSE">
<xsl:value-of
select="substring(concat(., ' '), 1, 7)"/>
</xsl:template>
<xsl:template match="NASAODOMETER">
<xsl:value-of
select="substring(concat(., ' '), 1, 6)"/>
</xsl:template>
<xsl:template match="SITEID"/>
</xsl:stylesheet>
这是我从这个文件中得到的输出
{
G10-1313K
GSA
G10
G101313K
73660
MP
G10-2465M
GSA
G10
G102465M
108590
MP
G10-2469M
GSA
G10
G102469M
78999
MP
}
似乎不起作用的是每条记录都不在一行上,我想跳过 xml 的前两个节点并从默认值 32M 开始,并且还想跳过最后一个元素 "MP".
在我的尝试中,我尝试了匹配,但没有指定任何内容来跳过我不想要的节点,我尝试创建一个变量并与之匹配,但输出似乎没有任何变化。
我不太精通 xslt,所以非常感谢帮助。
第一件事:如果你想要固定宽度的文本输出,那么不要使用恒等变换模板——那是没有意义的。
第二件事:输入的节点 XML 在命名空间中;因此,您的模板不会 select 任何东西,因为它们在无名称空间中寻址节点。你看到的结果完全是由 built-in template rules 产生的。使用完全没有模板的样式表会得到相同的结果。
现在,我无法理解您对所需输出的描述:您提供的数字与您显示的预期结果不符。试试这个作为你的起点:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mx="http://www.ibm.com/maximo">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="spaces" select="' '"/>
<xsl:template match="/mx:PublishNASAMILE">
<xsl:for-each select="mx:NASAMILESet/mx:ASSET">
<xsl:text>32M </xsl:text>
<xsl:value-of select="substring(concat(mx:NASAGSAVHCLCLASS, $spaces), 1, 4)"/>
<xsl:value-of select="substring(concat(mx:NASALICENSE, $spaces), 1, 10)"/>
<xsl:value-of select="substring(concat(mx:NASAODOMETER, $spaces), 1, 6)"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
请注意命名空间声明以及对输入文档中 select 个节点的关联前缀的使用。
应用于您的输入示例,结果将是:
32M G10 G101313K 73660
32M G10 G102465M 108590
32M G10 G102469M 78999