XSLT 1.0:选择和复制元素
XSLT 1.0: Selecting and Copying elements
我在 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 包含第 1、2、131 和 132 页
第一个复制语句中的选择器匹配小于或等于 2 的页面,
所以它将 return 第 1 页和第 2 页
第二个复制语句中的选择器匹配大于或等于 131,
的页面
所以它将 return 第 131 和 132 页
来自您在其他答案中的评论,原文中未明确显示post:
that's actually what I'm trying to do. I'm trying to split the "CV_Cover" into 2 distinct node, one with the pages with folio <= 2 and the other one with the pages who have folio >= 131.
考虑将子元素 book
在其父元素 books
的模板中拆分为两个,然后进行相应处理。
<?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"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="books[book/name='CV_Cover']">
<xsl:copy>
<xsl:variable name="Front" select="book/name/@FoliosFrontOK"/>
<xsl:variable name="Back" select="book/name/@FoliosBackOK"/>
<book>
<xsl:apply-templates select="book/@*"/>
<xsl:attribute name="folios">1-2</xsl:attribute>
<name FoliosFrontOK="1" FoliosBackOK="2">CV_Cover01</name>
<Pages>
<xsl:apply-templates select="book/Pages/Page[@nr <= $Front]" />
</Pages>
</book>
<book>
<xsl:apply-templates select="book/@*" />
<xsl:attribute name="folios">131-132</xsl:attribute>
<name FoliosFrontOK="131" FoliosBackOK="132">CV_Cover02</name>
<Pages>
<xsl:apply-templates select="book/Pages/Page[@nr >= $Back]" />
</Pages>
</book>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我在 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 包含第 1、2、131 和 132 页
第一个复制语句中的选择器匹配小于或等于 2 的页面,
所以它将 return 第 1 页和第 2 页
第二个复制语句中的选择器匹配大于或等于 131,
的页面
所以它将 return 第 131 和 132 页
来自您在其他答案中的评论,原文中未明确显示post:
that's actually what I'm trying to do. I'm trying to split the "CV_Cover" into 2 distinct node, one with the pages with folio <= 2 and the other one with the pages who have folio >= 131.
考虑将子元素 book
在其父元素 books
的模板中拆分为两个,然后进行相应处理。
<?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"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="books[book/name='CV_Cover']">
<xsl:copy>
<xsl:variable name="Front" select="book/name/@FoliosFrontOK"/>
<xsl:variable name="Back" select="book/name/@FoliosBackOK"/>
<book>
<xsl:apply-templates select="book/@*"/>
<xsl:attribute name="folios">1-2</xsl:attribute>
<name FoliosFrontOK="1" FoliosBackOK="2">CV_Cover01</name>
<Pages>
<xsl:apply-templates select="book/Pages/Page[@nr <= $Front]" />
</Pages>
</book>
<book>
<xsl:apply-templates select="book/@*" />
<xsl:attribute name="folios">131-132</xsl:attribute>
<name FoliosFrontOK="131" FoliosBackOK="132">CV_Cover02</name>
<Pages>
<xsl:apply-templates select="book/Pages/Page[@nr >= $Back]" />
</Pages>
</book>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>