2017-10-16 20 views
1

我有一個模板文件,其中有關的部分看起來像這樣:裝滿一個特定節點的轉義XML字符串的屬性和它的後代

<PipeSectionDefinition designation="Strang 1 RW/1-10" displayLastNode="true" drawMode="1" startPosition="0" pipeListId="xml:&lt;Pipes&gt;&#xD;&#xA; &lt;Pipe id=&quot;1A3352BC-8CFE-4D0C-BCCD-E30765B32821&quot; objectType=&quot;10000&quot; flowDirection=&quot;1&quot;/&gt;&#xD;&#xA; &lt;Pipe id=&quot;B9A3B241-C4DA-4925-BB4F-9564F645AEBD&quot; objectType=&quot;10000&quot; flowDirection=&quot;1&quot;/&gt;&#xD;&#xA;&lt;/Pipes&gt;&#xD;&#xA;" pipeLayer.ref="76037019-658c-41d5-b422-017f91e36701" nodeLayer.ref="03e6f262-bdb4-4684-b4bf-bb239feaad95"> 
    <CrossingPipes textVisible="true" lineVisible="true" lineWidth="0" positionVisible="true" symbolVisible="true" textOPath="Concat('DN ', RefObject.Cast(GeoObjectLineBase).Cast(GeoObjectLine01).Hoehe, ' SO: ', FormatNumber(GeoHeight, 3))" /> 
    <NodeConnections lineWidth="0" /> 
    <PipeConnections textVisible="true" lineVisible="true" lineWidth="0" positionVisible="true" symbolVisible="true" textOPath="Concat('DN ', Profilhoehe, ' SO: ', FormatNumber(RohrsohleAblauf, 3))" symbolLayer.ref="679e499d-022d-475e-bd76-8c7f7fa1aad8" textLayer.ref="679e499d-022d-475e-bd76-8c7f7fa1aad8" lineLayer.ref="679e499d-022d-475e-bd76-8c7f7fa1aad8" /> 
    <InletPipes lineWidth="0" /> 
    <OutletPipes lineWidth="0" symbolLayer.ref="03e6f262-bdb4-4684-b4bf-bb239feaad95" /> 
    <PipePoints textVisible="true" lineVisible="true" lineWidth="0" positionVisible="true" symbolVisible="true" lineLayer.ref="67877b9d-2de9-4764-9201-d02fbce8148f" /> 
    <SpecialPoints textVisible="true" lineVisible="true" lineWidth="0" positionVisible="true" symbolVisible="true" textOPath="FormatNumber(GeoHeight, 3)" /> 
    <TerrainPoints textVisible="true" lineVisible="true" lineWidth="0" positionVisible="true" symbolVisible="true" textOPath="FormatNumber(GeoHeight, 3)" lineLayer.ref="dbc6c898-6686-4be0-bc9c-9d15517258a5" /> 
    <AttributeSegments lineWidth="0" /> 
    <AllAttributes distanceBetweenSectionLineAndText="20" textHeight="1.8" /> 
</PipeSectionDefinition> 

可能有多個,這些PipeSectionDefintion元素,但我只是使用第一個作爲我的模板。有趣的部分是pipeListId屬性。

我的「數據源」是這樣的文件:

<StrangList> 
    <Pipes Bezeichnung="Strang 15b-SW" HaltungenOhneReihenfolge="0"> 
    <Pipe objectType="10000" flowDirection="1" Strang="Strang 15b-SW" Bezeichnung="7b - 7a" Reihenfolge="1" id="C9B982E1-82C0-4552-AF3D-03E3B9B8936A"/> 
    <Pipe objectType="10000" flowDirection="1" Strang="Strang 15b-SW" Bezeichnung="7a - 7" Reihenfolge="0" id="CF363D66-8C31-43C2-A5B7-575D3A6D02F5"/> 
    </Pipes> 
    <Pipes Bezeichnung="Strang 16a SW" HaltungenOhneReihenfolge="0"> 
    <Pipe objectType="10000" flowDirection="1" Strang="Strang 16a SW" Bezeichnung="3 - 2" Reihenfolge="2" id="37D88A6F-D449-4296-B696-AE9FA129F9E9"/> 
    <Pipe objectType="10000" flowDirection="1" Strang="Strang 16a SW" Bezeichnung="2 - 1" Reihenfolge="1" id="0693B8E5-108B-4877-BC02-10157C681FA3"/> 
    <Pipe objectType="10000" flowDirection="1" Strang="Strang 16a SW" Bezeichnung="1 - 6a" Reihenfolge="0" id="FC683422-DC7C-4050-865E-C87D4B49BD79"/> 
    </Pipes> 
</StrangList> 

首先,我想謀殺誰把一個轉義XML的文檔轉換成一個屬性值的人,但其次,我需要做同樣的事情發生,所以他的愚蠢程序做它應該做的。

偵測到的,該屬性的值是這樣的:

xml:<Pipes> 
    <Pipe id="1A3352BC-8CFE-4D0C-BCCD-E30765B32821" objectType="10000" flowDirection="1"/> 
    <Pipe id="B9A3B241-C4DA-4925-BB4F-9564F645AEBD" objectType="10000" flowDirection="1"/> 
</Pipes> 

因此,大家可以看到現在,我的「數據源」看起來幾乎一樣,屬性的倍數,有一些額外的屬性。如果他們來這裏旅行,這應該不成問題。所以我需要做的是將我的每個Pipes元素轉換爲一個轉義字符串,以「xml:」爲前綴,然後將其作爲該屬性的值。我現在的樣式看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 
    <xsl:import href="file:/C:/Java/xml-to-string.xsl"/> 
    <xsl:output indent="yes"/> 
    <xsl:variable name="Straenge" select="replace(document-uri(/), tokenize(document-uri(/), '/')[last()], 'Straenge.xml')"/> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" mode="local"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="PipeSections" mode="local"> 
     <xsl:variable name="template" select="PipeSectionDefinition[1]"/> 
     <xsl:for-each select="document($Straenge)/StrangList/Pipes"> 
      <xsl:variable name="Pipes" select="."/> 
      <xsl:variable name="PipesText"> 
       <xsl:call-template name="xml-to-string"> 
        <xsl:with-param name="node-set"><xsl:value-of select="$Pipes"/></xsl:with-param> 
       </xsl:call-template> 
      </xsl:variable> 
      <xsl:element name="PipeSectionDefinition"> 
       <xsl:attribute name="designation" select="@Bezeichnung"/> 
       <xsl:attribute name="displayLastNode" select="$template/@displayLastNode"/> 
       <xsl:attribute name="drawMode" select="$template/@drawMode"/> 
       <xsl:attribute name="startPosition" select="$template/@startPosition"/> 
       <xsl:attribute name="pipeListId"/> 
       <xsl:attribute name="pipeLayer.ref" select="$template/@pipeLayer.ref"/> 
       <xsl:attribute name="nodeLayer.ref" select="$template/@nodeLayer.ref"/> 
       <xsl:copy-of select="$template/child::node()"/> 
      </xsl:element> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

它使用從http://lenzconsulting.com/xml-to-string/的XML到string.xsl樣式相匹配/註釋掉的模板。變量$Straenge指向我的「數據源」文件,該文件位於正在轉換的文件的旁邊。

在此先感謝您的幫助,並讓我知道您是否需要更多信息!

+0

我沒有給你答案,但是我覺得你不得不在XML中嵌入轉義的XML。 – Steve

+0

那麼究竟是什麼問題,如果你有'xml-to-string.xsl'樣式表並調用它的模板來填充你的'PipesText'變量,爲什麼不用它的值來填充'pipeListId'屬性呢?一般來說,當您使用XSLT 2.0並且許多人使用Saxon來運行它時,請注意所有版本(包括HE)中的Saxon 9.8都支持XPath 3.0/3.1'serialize'函數,所以您甚至不需要導入用於序列化/ XML到字符串任務的樣式表。 –

+0

@MartinHonnen請添加,作爲一個實際的答案,因爲這已經解決了我的問題。而我的問題是,上面的樣式表沒有填充任何東西,不是我認爲它應該在當前狀態下的變量,也不是如果我改變它的屬性。但是'serialize()'是解決方案,因爲我使用了Saxon。 – Pipelynx

回答

2

正如你已經使用XSLT 2你可能使用撒克遜9作爲XSLT處理器和撒克遜9.8(在所有版本包括開源HE版)支持XSLT 3.0和中的XPath 3.0/3.1 serialize功能(https://www.w3.org/TR/xpath-functions-31/#func-serialize)你可能根本不需要導入的樣式表,但可以簡單地切換到最新的Saxon版本和XSLT 3.0並使用例如

<xsl:attribute name="pipeListId" select="'xml:' || serialize(.)"/> 

<xsl:for-each select="document($Straenge)/StrangList/Pipes"> 

內創建屬性值。

相關問題