[英]xsl: match newly created element from another xsl file
我創建了一個XSL文件,其中包含要在xml上執行的常見轉換。 該文件將包含在其他幾個XSL文件中,這些文件隨后將在這些通用規則之上添加更多模板。 我的問題是,我想匹配一個將由模板共同創建的元素,但是沒有擔保將在更具體的xsl需要匹配時創建它。 xsl:
common-stylesheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.standards.org/Intake"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"> <!-- xs namespace allows typed functions and parameters -->
<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*" />
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@*|text()|comment()|processing-instruction()">
<xsl:copy />
</xsl:template>
<!-- remove root -->
<xsl:template match="/*">
<xsl:apply-templates select="node()" />
</xsl:template>
<xsl:template match="OccurrenceData">
<xsl:element name="{local-name()}">
<xsl:element name="custom_Occurrence">
<!-- template adds 2 more elements to custom_Occurrence, omitted for brevity -->
<xsl:call-template name="createOccurrenceContent" />
</xsl:element>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- rest of stylesheet -->
</xsl:stylesheet>
first-specific-stylesheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:import href="common-stylesheet.xsl" />
<xsl:template match="custom_Occurrence">
<xsl:element name="{local-name()}">
<xsl:element name="custom_TestElement"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- rest of stylesheet -->
</xsl:stylesheet>
我正在通過first-specific-stylesheet發送以下輸入xml:
input.xml
<Request>
<RequestData>
<Occurrence>
<OccurrenceCd>EJ104</OccurrenceCd>
<!-- more children -->
</Occurrence>
<!-- rest of input-->
</RequestData>
</Request>
結果應如下所示:
required-output.xml
<RequestData>
<Occurrence>
<OccurrenceCd>EJ104</Occurrence>
<custom_Occurrence>
<custom_TestElement />
</custom_Occurrence>
<!--more children -->
</Occurrence>
<!-- rest of output -->
</RequestData>
此刻,通用模板已運行並且可以按預期運行,但是<custom_TestElement>
中缺少<custom_Occurrence>
。是否可以將通用文件包含到它們或特定的xsl文件中,並確保通用文件模板是在更具體的文件開始匹配新元素之前運行? 是否可以在一個文件中完成此操作,還是我必須將轉換分為自己的步驟,即先運行通用轉換,然后將xml傳遞到更具體的xml中?
注意:我正在使用<xsl:element name="{local-name()}">
和帶有額外模板格式的調整后的身份轉換,因為源xml沒有名稱空間,並且我在通用名稱中應用了一個xsl; 如果沒有這些內容,則將名稱空間應用於具有空白值或完整值的每個元素。 另外,我正在使用Saxon-HE v9.7.0-8進行轉換
編輯:我將無法將custom_OCCurrence的創建移至更特定的樣式表; 該元素塊及其內容在所有特定的樣式表中共享,每個元素塊都向該元素“塊”添加一個不同的元素和值,具體取決於使用哪個元素
您將需要使用一個變量,如
<xsl:variable name="temp">
<xsl:element name="custom_Occurrence">
<!-- template adds 2 more elements to custom_Occurrence, omitted for brevity -->
<xsl:call-template name="createOccurrenceContent" />
</xsl:element>
</xsl:variable>
<xsl:apply-templates select="$temp/node()"/>
如果要將模板應用於XSLT創建的節點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.