【发布时间】:2021-05-05 15:27:02
【问题描述】:
我没有太多使用 XSLT 的经验,而且我正在为复制元素而苦苦挣扎。 如果有人能指出我的错误,将不胜感激。
我正在尝试从顶部拆分 XML 文件中的特定节点。
因此,我匹配有问题的节点并在该节点内使用“副本”来匹配特定元素以重用。
问题是实现复制的时候,<Pages>后面多了一个<book>标签。
输入 XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<PartProducts>
<PartProduct name="PRI"
code="C"
pubCode="PRI"
pubDate="2021-01-26"
modifiedDate="2021-01-26T18:06:26.0"
zone="GP">
<books>
<book code="V" pageWidth="220" pageHeight="285" folios="1-2,131-132">
<name FoliosFrontOK="2" FoliosBackOK="131">CV_Cover</name>
<Pages>
<Page id="PRICVGP21012600101" nr="1" fullPageAd="false" zone="GP">
<PublishedAds/>
</Page>
<Page id="PRICVGP21012600102" nr="2" fullPageAd="true" zone="GP">
<PublishedAds>
<Ad id="206937-538053-813296"
x="0.0"
y="0.0"
width="220"
height="285"
bleed="true"
cols=""
system=""
materialPath="adv_dpp_ads/Adbase/Ad/56/100207756-02.pdf"
uaid="0000207756-02"/>
</PublishedAds>
</Page>
<Page id="PRICVGP21012600103" nr="131" fullPageAd="true" zone="GP">
<PublishedAds>
<Ad id="206737-537110-812091"
x="0.0"
y="0.0"
width="220"
height="285"
bleed="true"
cols="2"
system="DPPPROD"
materialPath="adv_dpp_ads/Adbase/Ad/56/100207556-08.pdf"
uaid="0000207556-08"/>
</PublishedAds>
</Page>
<Page id="PRICVGP21012600104" nr="132" fullPageAd="true" zone="GP">
<PublishedAds>
<Ad id="206249-535610-810275"
x="0.0"
y="0.0"
width="220"
height="285"
bleed="true"
cols=""
system=""
materialPath="adv_dpp_ads/Adbase/Ad/67/100207067-01.pdf"
uaid="0000207067-01"/>
</PublishedAds>
</Page>
</Pages>
</book>
</books>
</PartProduct>
<PartProduct name="PRI"
code="H"
pubCode="PRI"
pubDate="2021-01-26"
modifiedDate="2021-01-26T18:06:26.0"
zone="GP">
<books></books>
</PartProduct>
<PartProduct name="PRI"
code="H"
pubCode="PRI"
pubDate="2021-01-26"
modifiedDate="2021-01-26T18:06:26.0"
zone="GP">
<books></books>
</PartProduct>
</PartProducts>
输出 XML
<?xml version="1.0" encoding="UTF-8"?>
<PartProducts>
<PartProduct code="C"
modifiedDate="2021-01-26T18:06:26.0"
name="PRI"
pubCode="PRI"
pubDate="2021-01-26"
zone="GP">
<books>
<book>
<name>CV_Cover01</name>
<Pages>
<book>
<Page fullPageAd="false" id="PRICVGP21012600101" nr="1" zone="GP">
<PublishedAds/>
</Page>
<Page fullPageAd="true" id="PRICVGP21012600102" nr="2" zone="GP">
<PublishedAds>
<Ad bleed="true"
cols=""
height="285"
id="206937-538053-813296"
materialPath="adv_dpp_ads/Adbase/Ad/56/100207756-02.pdf"
system=""
uaid="0000207756-02"
width="220"
x="0.0"
y="0.0"/>
</PublishedAds>
</Page>
</book>
</Pages>
</book>
<book>
<name>CV_Cover02</name>
<Pages>
<book>
<Page fullPageAd="true" id="PRICVGP21012600103" nr="131" zone="GP">
<PublishedAds>
<Ad bleed="true"
cols="2"
height="285"
id="206737-537110-812091"
materialPath="adv_dpp_ads/Adbase/Ad/56/100207556-08.pdf"
system="DPPPROD"
uaid="0000207556-08"
width="220"
x="0.0"
y="0.0"/>
</PublishedAds>
</Page>
<Page fullPageAd="true" id="PRICVGP21012600104" nr="132" zone="GP">
<PublishedAds>
<Ad bleed="true"
cols=""
height="285"
id="206249-535610-810275"
materialPath="adv_dpp_ads/Adbase/Ad/67/100207067-01.pdf"
system=""
uaid="0000207067-01"
width="220"
x="0.0"
y="0.0"/>
</PublishedAds>
</Page>
</book>
</Pages>
</book>
</books>
</PartProduct>
<PartProduct code="H"
modifiedDate="2021-01-26T18:06:26.0"
name="PRI"
pubCode="PRI"
pubDate="2021-01-26"
zone="GP">
<books></books>
</PartProduct>
<PartProduct code="H"
modifiedDate="2021-01-26T18:06:26.0"
name="PRI"
pubCode="PRI"
pubDate="2021-01-26"
zone="GP">
<books></books>
</PartProduct>
</PartProducts>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tns="http://www.myproject/SS/PMSchema"
exclude-result-prefixes="tns"
version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="book[name='CV_Cover']">
<xsl:variable name="Front" select="name/@FoliosFrontOK"/>
<xsl:variable name="Back" select="name/@FoliosBackOK"/>
<book>
<name>CV_Cover01</name>
<Pages>
<xsl:copy>
<xsl:apply-templates select="Pages/Page[@nr <= $Front]" />
</xsl:copy>
</Pages>
</book>
<book>
<name>CV_Cover02</name>
<Pages>
<xsl:copy>
<xsl:apply-templates select="Pages/Page[@nr >= $Back]" />
</xsl:copy>
</Pages>
</book>
</xsl:template>
<!-- Select everything from original xml and copy it back + apply all templates -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
因此,在输入 xml 中,我们有 books/book/Pages/Page,在 XSLT 之后,我得到 books/book/Pages/book/Page 知道我做错了什么,或者如何摆脱这个特定的标签吗?
【问题讨论】:
-
最后一个问题,什么是特定标签,因为您显示了许多标签?请显示所需的 XML 结果。