我生成了一个报告,该报告以一对多的方式显示记录。但是,我需要将它们显示为一个。我使用XSLT转换了源XML,但是没有用。
我开发了此XSLT来转换XML。
但是,它不起作用。
这是我的XML:
<?xml version="1.0" encoding="utf-16"?>
<Records count="2">
<Record contentId="1442264" levelId="98" levelGuid="b085b230-e20f-41df-
a849-f5d6811447ea" moduleId="167" parentId="0">
<Record contentId="1608202" levelId="155" levelGuid="20b8e343-96c0-4aed-
804c-7e40b489f31b" moduleId="537" parentId="0">
<Field id="17169" guid="ed20bfb9-d2e9-44c2-9b22-5e39d26beae4"
type="6">16913</Field>
<Field id="27556" guid="d378d42e-42da-4a23-906a-722fcb7d761e"
type="6">1608202</Field>
</Record>
<Record contentId="1608204" levelId="155" levelGuid="20b8e343-96c0-4aed-
804c-7e40b489f31b" moduleId="537" parentId="0">
<Field id="17169" guid="ed20bfb9-d2e9-44c2-9b22-5e39d26beae4"
type="6">16915</Field>
<Field id="27556" guid="d378d42e-42da-4a23-906a-722fcb7d761e"
type="6">1608204</Field>
</Record>
<Field id="2260" guid="a69370c5-b6a7-4e20-a073-dd6bbd131e43"
type="6">1442264</Field>
</Record>
<Record contentId="1445144" levelId="98" levelGuid="b085b230-e20f-41df-
a849-f5d6811447ea" moduleId="167" parentId="0">
<Record contentId="1608203" levelId="155" levelGuid="20b8e343-96c0-4aed-
804c-7e40b489f31b" moduleId="537" parentId="0">
<Field id="17169" guid="ed20bfb9-d2e9-44c2-9b22-5e39d26beae4"
type="6">16914</Field>
<Field id="27556" guid="d378d42e-42da-4a23-906a-722fcb7d761e"
type="6">1608203</Field>
</Record>
<Record contentId="1608205" levelId="155" levelGuid="20b8e343-96c0-4aed-
804c-7e40b489f31b" moduleId="537" parentId="0">
<Field id="17169" guid="ed20bfb9-d2e9-44c2-9b22-5e39d26beae4"
type="6">16916</Field>
<Field id="27556" guid="d378d42e-42da-4a23-906a-722fcb7d761e"
type="6">1608205</Field>
</Record>
<Field id="2260" guid="a69370c5-b6a7-4e20-a073-dd6bbd131e43"
type="6">1445144</Field>
</Record>
<Metadata>
<FieldDefinitions>
<FieldDefinition id="2260" guid="a69370c5-b6a7-4e20-a073-dd6bbd131e43"
name="Issue ID" alias="Finding_ID" />
<FieldDefinition id="17169" guid="ed20bfb9-d2e9-44c2-9b22-5e39d26beae4"
name="CAP ID" alias="Remediation_Plan_ID" />
<FieldDefinition id="17182" guid="e950ebab-1bf6-4fc8-818b-88a54fd12f89"
name="CAP Title" alias="Name" />
<FieldDefinition id="27556" guid="d378d42e-42da-4a23-906a-722fcb7d761e"
name="X - Tracking ID" alias="X__Tracking_ID" />
</FieldDefinitions>
</Metadata>
<LevelCounts>
<LevelCount id="98" guid="b085b230-e20f-41df-a849-f5d6811447ea" count="2"
/>
<LevelCount id="155" guid="20b8e343-96c0-4aed-804c-7e40b489f31b"
count="4" />
</LevelCounts>
</Records>
这是我的XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns="http://www.archer-tech.com/">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<ArcherRecords>
<xsl:for-each select="Records/Record/Field[@guid='b085b230-e20f-41df-
a849-f5d6811447ea']/Reference">
<ArcherRecord>
<IssueID>
<xsl:value-of select="."/>
</IssueID>
<CAPID>
<xsl:value-of select="../../Field[@guid='ed20bfb9-d2e9-44c2-9b22-
5e39d26beae4']"/>
</CAPID>
<XTrackingID>
<xsl:value-of select="../../Field[@guid='d378d42e-42da-
4a23-906a-722fcb7d761e']"/>
</XTrackingID>
</ArcherRecord>
</xsl:for-each>
</ArcherRecords>
</xsl:template>
</xsl:stylesheet>
我的输入是:
Issue ID CAP ID X - Tracking ID
Issue-1442264 CAP-16913 1608202
CAP-16915 1608204
Issue-1445144 CAP-16914 1608203
CAP-16916 1608205
预期输出为:
Issue ID CAP ID X - Tracking ID
Issue-1442264 CAP-16913 1608202
Issue-1442264 CAP-16915 1608204
Issue-1445144 CAP-16914 1608203
Issue-1445144 CAP-16916 1608205
实际输出为:
<?xml version="1.0" encoding="UTF-8"?>
<ArcherRecords xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.archer-tech.com/"/>
答案 0 :(得分:0)
XSLT失败的主要原因是XML和XSLT都有不正确的空格处理。因此,表达式不匹配!
解决方案是修复换行符!
例如:
您相关的selectedProducts = [
{
id: 12,
sku: 12064273040195392,
title: "Cat Tee Black T-Shirt",
availableSizes: ["XS", "S"],
style: "Black with custom print",
price: 10.9,
imgUrl: photo1,
currencyId: "GBP",
currencyFormat: "£",
isFreeShipping: true
},
{
id: 13,
sku: 51498472915966370,
title: "Dark Thug Blue-Navy T-Shirt",
availableSizes: ["M"],
style: "Front print and paisley print",
price: 29.45,
imgUrl: photo2,
currencyId: "GBP",
currencyFormat: "£",
isFreeShipping: true
},
{
id: 12,
sku: 12064273040195392,
title: "Cat Tee Black T-Shirt",
availableSizes: ["XS", "S"],
style: "Black with custom print",
price: 10.9,
imgUrl: photo1,
currencyId: "GBP",
currencyFormat: "£",
isFreeShipping: true
} ];
属性包含一个换行符和一些空格,这使得它(几乎)无法与连接的XPath表达式的字符串值匹配。
因此,您将不得不面对不方便的任务来更正XML的换行符。
解决断行问题后,以下XSLT-1.0代码将为您提供一些有用的输出:
levelGuid
其输出为:
<xsl:template match="/Records">
<ArcherRecords>
<xsl:for-each select="Record[@levelGuid='b085b230-e20f-41df-a849-f5d6811447ea']">
<ArcherRecord>
<IssueID>
<xsl:value-of select="@contentId"/>
</IssueID>
<xsl:for-each select="Record">
<CAPID>
<xsl:value-of select="Field[@guid='ed20bfb9-d2e9-44c2-9b22-5e39d26beae4']"/>
</CAPID>
<XTrackingID>
<xsl:value-of select="Field[@guid='d378d42e-42da-4a23-906a-722fcb7d761e']"/>
</XTrackingID>
</xsl:for-each>
</ArcherRecord>
</xsl:for-each>
</ArcherRecords>
</xsl:template>
这是您问题的答案。
现在,剩下的任务是获取纯文本输出。
创建所需输出的模板是:
<?xml version="1.0"?>
<ArcherRecords xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:arch="http://www.archer-tech.com/">
<ArcherRecord>
<IssueID>1442264</IssueID>
<CAPID>16913</CAPID>
<XTrackingID>1608202</XTrackingID>
<CAPID>16915</CAPID>
<XTrackingID>1608204</XTrackingID>
</ArcherRecord>
<ArcherRecord>
<IssueID>1445144</IssueID>
<CAPID>16914</CAPID>
<XTrackingID>1608203</XTrackingID>
<CAPID>16916</CAPID>
<XTrackingID>1608205</XTrackingID>
</ArcherRecord>
</ArcherRecords>
其输出为:
<xsl:output method="text" indent="yes" />
<xsl:template match="/Records">
<xsl:text>Issue ID	CAP ID		X - Tracking ID
</xsl:text>
<xsl:for-each select="Record[@levelGuid='b085b230-e20f-41df-a849-f5d6811447ea']">
<xsl:for-each select="Record">
<xsl:value-of select="concat('Issue-',../@contentId)"/><xsl:text>	</xsl:text>
<xsl:value-of select="concat('CAP-',Field[@guid='ed20bfb9-d2e9-44c2-9b22-5e39d26beae4'])"/><xsl:text>	</xsl:text>
<xsl:value-of select="Field[@guid='d378d42e-42da-4a23-906a-722fcb7d761e']"/>
<xsl:text>	
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
答案 1 :(得分:0)
您只需要找到一对多XML关系中的image_id
,然后迭代one
即可生成新的XML元素。
.xsl
many
根据您的XML得出的结果是
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl" xmlns="http://www.archer-tech.com/">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<ArcherRecords>
<xsl:for-each select='//Record[@levelGuid="b085b230-e20f-41df-a849-f5d6811447ea"]'>
<xsl:param name="issueid">
<xsl:value-of select="Field[@guid='a69370c5-b6a7-4e20-a073-dd6bbd131e43']"></xsl:value-of>
</xsl:param>
<xsl:for-each select="Record">
<ArcherRecord>
<IssueID>Issue-<xsl:value-of select="$issueid"></xsl:value-of>
</IssueID>
<CAPID>CAP-<xsl:value-of select="Field[@guid='ed20bfb9-d2e9-44c2-9b22-5e39d26beae4']" />
</CAPID>
<XTrackingID>
<xsl:value-of select="Field[@guid='d378d42e-42da-4a23-906a-722fcb7d761e']" />
</XTrackingID>
</ArcherRecord>
</xsl:for-each>
</xsl:for-each>
</ArcherRecords>
</xsl:template>
</xsl:stylesheet>
固定的换行符XML:
<ArcherRecords>
<ArcherRecord>
<IssueID>Issue-1442264</IssueID>
<CAPID>CAP-16913</CAPID>
<XTrackingID>1608202</XTrackingID>
</ArcherRecord>
<ArcherRecord>
<IssueID>Issue-1442264</IssueID>
<CAPID>CAP-16915</CAPID>
<XTrackingID>1608204</XTrackingID>
</ArcherRecord>
<ArcherRecord>
<IssueID>Issue-1445144</IssueID>
<CAPID>CAP-16914</CAPID>
<XTrackingID>1608203</XTrackingID>
</ArcherRecord>
<ArcherRecord>
<IssueID>Issue-1445144</IssueID>
<CAPID>CAP-16916</CAPID>
<XTrackingID>1608205</XTrackingID>
</ArcherRecord>
</ArcherRecords>