문제
상황:
나는 간단한 XML 이미지 정보가 포함된 문서입니다.로 변환해야 해요 HTML.그러나 오픈 태그가 어디에 있는지, 언제 사용하는지 알 수 없습니다. XSL 아래 코드에서는 다음과 같은 오류 메시지가 표시됩니다.
"요소 시작 태그가 열려 있지 않으면 속성 노드를 쓸 수 없습니다."
XML 콘텐츠:
<root>
<HeaderText>
<HeaderText>Dan Testing</HeaderText>
</HeaderText>
<Image>
<img width="100" height="100" alt="FPO lady" src="/uploadedImages/temp_photo_small.jpg"/>
</Image>
<BodyText>
<p>This is a test of the body text<br /></p>
</BodyText>
<ShowLinkArrow>false</ShowLinkArrow>
</root>
XSL 코드:
<xsl:stylesheet version="1.0" extension-element-prefixes="msxsl"
exclude-result-prefixes="msxsl js dl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:js="urn:custom-javascript" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:dl="urn:datalist">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
<xsl:template match="/" xml:space="preserve">
<img>
<xsl:attribute name="width">
100
</xsl:attribute>
<xsl:attribute name="height">
100
</xsl:attribute>
<xsl:attribute name="class">
CalloutRightPhoto
</xsl:attribute>
<xsl:attribute name="src">
<xsl:copy-of select="/root/Image/node()"/>
</xsl:attribute>
</img>
</xsl:template>
</xsl:stylesheet>
해결책
여기서 문제를 명확히 하기 위해 다음 코드에 오류가 있습니다.
<xsl:attribute name="src">
<xsl:copy-of select="/root/Image/node()"/>
</xsl:attribute>
xsl:copy-of 명령은 노드 또는 노드 집합을 가져와서 복사본을 만들어 노드 또는 노드 집합을 출력합니다.그러나 속성은 노드를 포함할 수 없고 텍스트 값만 포함할 수 있으므로 xsl:value-of가 가능한 솔루션이 될 수 있습니다(노드 또는 노드 세트의 텍스트 값을 반환하므로).
훨씬 더 짧은 솔루션(아마도 더 우아한 솔루션)은 다음과 같습니다.
<img width="100" height="100" src="{/root/Image/node()}" class="CalloutRightPhoto"/>
특성에서 {}를 사용하는 것을 특성 값 템플릿이라고 하며 모든 XPATH 표현식을 포함할 수 있습니다.
속성 값 템플릿에서 사용될 때 텍스트 값을 취하는 것으로 알고 있으므로 xsl_copy-of에서 사용한 것과 동일한 XPath를 여기에서 사용할 수 있습니다.
다른 팁
그렇지 않습니까?
<xsl:value-of select="/root/Image/img/@src"/>
?전체 Image/img 노드를 @src 속성에 복사하려는 것 같습니다.
속성을 추가하기 위해 XSL은
<xsl:element name="img"> (attributes) </xsl:element>
그냥 대신에
<img> (attributes) </img>
그렇습니다. 요소를 있는 그대로 복사하는 경우에는 그런 것이 필요하지 않습니다.
신경 쓰지 마세요. 저는 바보입니다.난 그냥 필요했어요 <xsl:value-of select="/root/Image/node()"/>
시도할 수 있는 다른 옵션은 간단합니다.
<img width="100" height="100" src="/root/Image/image.jpeg" class="CalloutRightPhoto"/>
즉.{} 없이 대신 직접 이미지 경로를 제공합니다.